Skip to content

Instantly share code, notes, and snippets.

@belichuk
Created June 24, 2014 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belichuk/dd3d15fbbfb4622008f0 to your computer and use it in GitHub Desktop.
Save belichuk/dd3d15fbbfb4622008f0 to your computer and use it in GitHub Desktop.
patTemplate vs. Twig
<?php
function microtime_float()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
require_once "../sqlconn.php";
$params = array(
'doctype' => '<!DOCTYPE html>',
'title' => 'Demo application',
'script' => 'console.log("Demo application");',
'body' => '<h1>Demo application</h1>'
);
$i = 0;
$attempts = 1000;
$twig = new TwigLoad(dirname(__FILE__), false);
$oPage = new patTemplate_v2();
$oPage->setBasedir("./");
$oPage->readTemplatesFromFile("test.tmpl.html");
$oPage->addVars('page', $params);
$time_start1 = microtime_float();
for ($i=0;$i<$attempts;$i++) {
$twigContent = $twig->render('test.twig', $params);
}
$time_end1 = microtime_float();
$time_start2 = microtime_float();
for ($i=0;$i<$attempts;$i++) {
$patContent = $oPage->getParsedTemplate('page');
}
$time_end2 = microtime_float();
echo 'twig render time: ' . ($time_end1 - $time_start1) . '<br />';
echo 'patT render time: ' . ($time_end2 - $time_start2) . '<br />';
<patTemplate:tmpl name="page">
{DOCTYPE}
<html>
<head>
<title>{TITLE}</title>
</head>
<body>
<script type="text/javascript">
{SCRIPT}
</script>
{BODY}
</body>
</html>
</patTemplate:tmpl>
{{ doctype | raw }}
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<script type="text/javascript">
{{ script | raw }}
</script>
{{ body | raw }}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment