PHPãã³ãã¬ã¼ãã¨ã³ã¸ã³ãã³ããã¼ã¯ ãã¼ã2 Smarty vs PHPTAL
以åPHPã®ãã³ãã¬ã¼ãã¨ã³ã¸ã³ã®å®è¡é度ãæ¯è¼ãã¾ãããã
http://d.hatena.ne.jp/tanakahisateru/20081024/1224850023
ãã¾ããã¾ãè¨æ¸¬ã§ãã¦ããªãã£ãã®ã¨ãã½ã¼ã¹å ¬éãã¦ãªãã£ãã®ã¨ãããããã使ã£ããã·ã³ãå¤ããªã£ã¦ããæããã£ãã¨ãã3ã¤ã®çç±ããããã¾ãã¡ã©ããã¡ãã¨ããç´ãããã¨æãã¾ãã
PHPã®ãã¼ã¸ã§ã³ã¯5.3.3ã§ã2.4GHz Core2ã®MacBookã§ããä»ã©ãããããããªãã¡ãããPHPTALãç¾è¡ãã¼ã¸ã§ã³ã«ä¸ãã¾ãããã
HTML
åèç¨ã§ã¹ã¿ãã£ãã¯HTMLã§ãã以éã®PHPã¯ããã¨åãçµæãçæãã¾ãã(ã¤ã³ãã³ããæ¹è¡ã¯å³å¯ã«åç¾ããã¾ããããåå¼ãã¦ãã ãã)
<html> <head> <title>The title value</title> </head> <body> <h1>The title value</h1> <table> <thead> <tr><th></th><th>Name</th><th>Phone</th></tr> </thead> <tbody> <tr> <td>0</td> <td>foo</td> <td>01-344-121-021</td> </tr> <tr> <td>1</td> <td>bar</td> <td>05-999-165-541</td> </tr> <tr> <td>2</td> <td>baz</td> <td>01-389-321-024</td> </tr> <tr> <td>3</td> <td>quz</td> <td>05-321-378-654</td> </tr> </tbody> </table> </body> </html>
PHP
çã®PHPã§ããããããã¨ã表ãã¾ãã
<?php // the Person class class Person { public $name; public $phone; function Person($name, $phone) { $this->name = $name; $this->phone = $phone; } } // let's create an array of objects for test purpose $people = array(); $people[] = new Person("foo", "01-344-121-021"); $people[] = new Person("bar", "05-999-165-541"); $people[] = new Person("baz", "01-389-321-024"); $people[] = new Person("quz", "05-321-378-654"); $title = 'The title value'; // execute the template require "my_template_file.php"; ?>
my_template_file.php
<html> <head> <title> <?php echo htmlspecialchars($title); ?> </title> </head> <body> <h1><?php echo htmlspecialchars($title); ?></h1> <?php if(!empty($people)): ?> <table> <thead> <tr><th></th><th>Name</th><th>Phone</th></tr> </thead> <tbody> <?php foreach($people as $idx=>$person): ?> <tr> <td><?php echo $idx; ?></td> <td><?php echo htmlspecialchars($person->name); ?></td> <td><?php echo htmlspecialchars($person->phone); ?></td> </tr> <?php endforeach; ?> </tbody> </table> <?php else: ?> <p>No people exists.</p> <?php endif; ?> </body> </html>
Smarty
ãã¼ã¸ã§ã³ 2.6.7
<?php require "Smarty/libs/Smarty.class.php"; // the Person class class Person { public $name; public $phone; function Person($name, $phone) { $this->name = $name; $this->phone = $phone; } } // let's create an array of objects for test purpose $people = array(); $people[] = new Person("foo", "01-344-121-021"); $people[] = new Person("bar", "05-999-165-541"); $people[] = new Person("baz", "01-389-321-024"); $people[] = new Person("quz", "05-321-378-654"); // create a new template object $smarty = new Smarty(); // put some data into the template context $smarty->assign('title', 'The title value'); $smarty->assign('people', $people); // execute the template $smarty->display('my_template_file.tpl'); ?>
my_template_file.tpl
<html> <head> <title> {$title|escape:'html'} </title> </head> <body> <h1>{$title|escape:'html'}</h1> {if not empty($people) } <table> <thead> <tr><th></th><th>Name</th><th>Phone</th></tr> </thead> <tbody> {foreach from=$people key=idx item=person} <tr> <td>{$idx}</td> <td>{$person->name|escape:'html'}</td> <td>{$person->phone|escape:'html'}</td> </tr> {/foreach} </tbody> </table> {else} <p>No people exists.</p> {/if} </body> </html>
PHPTAL
ãã¼ã¸ã§ã³ 1.2.1
<?php require_once 'PHPTAL.php'; // the Person class class Person { public $name; public $phone; function Person($name, $phone) { $this->name = $name; $this->phone = $phone; } } // let's create an array of objects for test purpose $people = array(); $people[] = new Person("foo", "01-344-121-021"); $people[] = new Person("bar", "05-999-165-541"); $people[] = new Person("baz", "01-389-321-024"); $people[] = new Person("quz", "05-321-378-654"); // create a new template object $template = new PHPTAL('my_template_file.html'); // put some data into the template context $template->title = 'The title value'; $template->people = $people; // execute the template try { echo $template->execute(); } catch (Exception $e){ header("Content-Type:text/plain"); echo $e; } ?>
my_template_file.html
<html> <head> <title tal:content="title"> Place for the page title </title> </head> <body> <h1 tal:content="title">sample title</h1> <table tal:condition="people"> <thead> <tr><th></th><th>Name</th><th>Phone</th></tr> </thead> <tbody> <tr tal:repeat="person people"> <td tal:content="repeat/person/key">idx</td> <td tal:content="person/name">person's name</td> <td tal:content="person/phone">person's phone</td> </tr> </tbody> </table> <p tal:condition="not: people">No people exists.</p> </body> </html>
é·ããããããã¾ãããã¾ãããããªæãã§ãããã©ã¯ã¡ããã¨ab使ã£ã¦ã¯ã©ã¤ã¢ã³ãå´ã®ç¡é§ããªããã¦ããã¾ããã以ä¸ã1000ãªã¯ã¨ã¹ããå¦çããã®ã«ããã£ãæéã§ãã1ãªã¯ã¨ã¹ããããã®
æéã¯ã1000ã§å²ã£ã¦èãã¦ãã ããã5000ms/1000reqãªãã5ms/reqã§ãã
APCãªã
ã·ã¼ã±ã³ã·ã£ã« | æ¥ç¶10æ¬ä¸¦å | |
HTML | 314ms | 232ms |
PHP | 852ms | 554ms |
Smarty | 4491ms | 2512ms |
PHPTAL | 5122ms | 2848ms |
Smartyã®æè¦æéã100%ã¨ããã¨ããPHPTALã¯ãã·ã¼ã±ã³ã·ã£ã«ã®å ´åã§+14%ã10æ¬æ¥ç¶ã§+13%ã§ãããã¾ãã¡ãã£ã¨æ°ã«ãªããããã§ãã
APCãã
ã·ã¼ã±ã³ã·ã£ã« | æ¥ç¶10æ¬ä¸¦å | |
PHP | 632ms | 406ms |
Smarty | 1160ms | 682ms |
PHPTAL | 2033ms | 1146ms |
APCããã ã¨Smartyããã³ããåªç§ã§ãããPHPTALã®æè¦æéããã·ã¼ã±ã³ã·ã£ã«ã§+75%ã10æ¬æ¥ç¶ã§+68%ã«ãªãã¾ãããããã¯æ¯ããã¨ã³ã¸ã³ããã£ãã·ã¥ãå¹ããå ´åã®å¹æãèãã¦ããããã§ããã§ãSmartyã¨PHPã§ç«¶ããã¦ã¾ãããã©ã¡ãã®ã¨ã³ã¸ã³ããAPCã使ãã¨çã®PHPã«ããã ãè¿«ãããã ã¨ãããã¨ã®ã»ããé¢ç½ãããããªããã¨æãã¾ãã
ããã©ã¼ãã³ã¹ã§æ¯ã¹ãã°ãPHPTALããã¯Smartyã®ã»ããé«éã§ãããã§ããããã ãæ§æããã°ã£ã¦ãã®ã«ããã®ç¨åº¦ã®å·®ã§æ¸ãã§ããã¨ããã®ã¯ãç´ æ´ããããã¨ã§ããã½ã¼ã¹ã®DOMãå£ããªããããããã¹ããã¼ãã«ã³ã¼ãããªãã
<tbody> {foreach from=$people key=idx item=person} <tr>
<tbody> <tr tal:repeat="person people">
Dreamweaverã§éããã¨èãã¦ã¿ã¦ãã ããããã¶ã¤ãã¼ãã©ãã ãåã¶ãã¨ããããããã»ãã¨ãã³ã¼ãã®ç¶ºéºãã«é©ãã¦ãæéãã¼ãã¨è¨ã£ã¦ããããããããã«ä»äºãã¦ãããã¾ããããã¶ã¤ã³ä½æ¥ãã¦ãéãããã°ã©ãã¼ã¯ã»ã¨ãã©è³ªåããã¾ããããããå®ä½é¨ã
ãããªãããªã§ãèªåã«ã¨ã£ã¦ã®ãã©ã¤ããªã¼ãã³ãã¬ã¼ãã¨ã³ã¸ã³ã¯ããã£ã±ãPHPTALã§ããåã®å ´åãæ¬å½ã®æ¬å½ã«å®è¡é度ãå¿ è¦ãªå ´åã¯ããã¶ã¤ã³ä½æ¥ãããªã¼ãºãã¦ãçã®PHPããã£ã¤ãæ¸ãã§ãããããSmartyæ¸ãã®ãPHPãã¼ã¸æ¸ãã®ãããããªã«éããªãã®ã§ãSmartyã§ä¸éå端ãªé度ã«ãããªããªããªãããã£ããããªãPHPãã¨ããèãæ¹ã