<?php
if ('php' == $_GET['showsource'])
{
show_source($_SERVER['SCRIPT_FILENAME']);
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<!-- ### Titel, Meta-Angaben und Einbindung der Stylesheets ### -->
<title>Benchmark 2: loop support</title>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<link rel="stylesheet" href="info.css" type="text/css" />
</head>
<body>
<h1>Benchmark 2: loop support</h1>
<p>About 1MB data is created, parsed and displayed ... this might take a while. View <a href="tpl_bench_2.php?showsource=php">source code</a> of this benchmark.</p>
<hr />
<?php
flush();
//set_time_limit(3600);
include_once('benchmark.class.php');
$bench = new benchmark();
require_once('php_heredoc/search_and_replace_with_php.php');
define('SMARTY_DIR', 'smarty/smarty-2.6.9/');
require_once(SMARTY_DIR . 'Smarty.class.php');
require_once('ets/ets.php');
require_once('tbs/tbs_class.php');
require_once('vlib/vlibTemplate.php');
require_once('patTemplate/patTemplate.php');
$run_2_1 = 10;
mt_srand((double)microtime()*1000000);
$templates = array(
'php_heredoc' => 'PHP: HereDoc syntax',
'smarty' => 'Smarty',
'vlib' => 'vlibTemplate',
'ets' => 'Easy Template System',
'tbs' => 'TinyButStrong',
'pattemplate' => 'patTemplate'
);
// build global loop
for ($i = 0; $i < 100; $i++)
{
$tmp = array(
'id' => 20 + $i,
'name' => "longname$i",
'param1' => 'Random: ' . mt_rand(),
'param2' => "PARAMETER #$i - powered by ARRAY",
'param3' => 'hello world'
);
$loop[] = $tmp;
}
// build loop for ETS (Easy Template System)
for ($i = 0; $i < 100; $i++)
{
$ets_loop->loop[$i]->id = 20 + $i;
$ets_loop->loop[$i]->name = "longname$i";
$ets_loop->loop[$i]->param1 ='Random: ' . mt_rand();
$ets_loop->loop[$i]->param2 = "PARAMETER #$i - powered by ARRAY";
$ets_loop->loop[$i]->param3 = 'hello world';
}
// build loop for patTemplate
for ($i = 0; $i < 100; $i++)
{
$id[$i] = 20 + $i;
$name[$i] = "longname$i";
$param1[$i] = 'Random: ' . mt_rand();
$param2[$i] = "PARAMETER #$i - powered by ARRAY";
$param3[$i] = 'hello world';
}
foreach( $templates as $id => $name )
{
$bench->run($run_2_1, $id.'_2_1' );
$bench->add_result($bench->get(), $name);
}
$bench->stats();
$bench->print_stats("TEST 2.1 :: text 1kb // loop 100 records // $run_2_1 iterations");
$bench->reset();
/**********************************************************
** RUN 2.1 **********************************************
***********************************************************/
function php_heredoc_2_1()
{
global $loop;
loop_search_and_replace_with_php($loop);
}
function smarty_2_1()
{
global $loop;
$tpl = new Smarty;
$tpl->template_dir = 'smarty/templates';
$tpl->compile_dir = 'smarty/templates_c';
$tpl->compile_check = TRUE;
$tpl->assign('loop', $loop);
$tpl->display('test_2_1.html');
}
function ets_2_1()
{
global $ets_loop;
printt($ets_loop, 'ets/test_2_1.html');
}
function vlib_2_1()
{
global $loop;
$tpl = new vlibTemplateCache('vlib/test_2_1.html');
$tpl->setLoop('loop', $loop);
$tpl->pparse();
}
function tbs_2_1()
{
global $loop;
$tmpl = new clsTinyButStrong;
$tmpl->LoadTemplate('tbs/test_2_1.html');
$tmpl->MergeBlock('loop', $loop);
$tmpl->Show(TBS_OUTPUT);
}
function pattemplate_2_1()
{
global $id, $name, $param1, $param2, $param3;
$tmpl = new patTemplate();
$tmpl->setBasedir('patTemplate');
$tmpl->readTemplatesFromFile('test_2_1.html');
$tmpl->addVars('loop', array("ID" => $id, "NAME" => $name, "PARAM1" => $param1, "PARAM2" => $param2, "PARAM3" => $param3));
$tmpl->displayParsedTemplate();
}
?>
<p>To show the speed of template engines, I integrated the PHP heredoc syntax.</p>
<p>Why is ETS so fast? ETS does not work with <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" target="_blank">OOP</a>. It uses functions not methods to exchange data between the PHP script and the template. I can only assume this different aproach to programming is a reason for the high performance.</p>
<p>Why are Smarty and <a href="/" target="_blank">vlibTemplate</a> so fast? Smarty and <a href="/" target="_blank">vlibTemplate</a> are using <b>compiled templates</b>. E.g. Smarty PHP files are using fast "for ... endfor;" and "echo" statements. Since this PHP file is only changed if the template is changed, this is extremly fast <b>and it does not matter if the (mysql)data is changed</b>. This makes Smarty the fastes template engine available on Windows. <a href="/" target="_blank">vlibTemplate</a> is faster than Smarty on most Linux servers I tested. But that may change, since Smarty is released more often than vLIB.</p>
<p>If you do not have the time to take a deeper look into Smarty, ETS or other <a href="http://en.wikipedia.org/wiki/Template_engine" target="_blank">template engines</a> or <a href="/" target="_blank">vlibTemplate</a> I suggest you use <a href="/" target="_blank">vlibTemplate</a>. In my humble opinion vLIB (<a href="/docs/multihtml/vlibtemplate/table_of_content.html" target="_blank">vlibTemplate</a>, <a href="/docs/multihtml/vlibdate/table_of_content.html" target="_blank">vlibDate</a>, <a href="/docs/multihtml/vlibmimemail/table_of_content.html" target="_blank">vlibMimeMail</a>) is one of the best packages (3 classes) I have ever worked with.</p>
</body>
</html>