Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Smarty speed benchmark
Goto page Previous  1, 2
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Feb 29, 2016 7:29 pm    Post subject: Reply with quote

mohrt wrote:
What if you use a template file instead of a string? I have a hunch the compiler will need to evaluate and compile the template string on each invocation of the loop.

Download my archive and play with it as you wish.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Mar 01, 2016 12:12 am    Post subject: Reply with quote

mohrt wrote:
What if you use a template file instead of a string? I have a hunch the compiler will need to evaluate and compile the template string on each invocation of the loop.

That is not the case.

The template object was not cached on $smarty->fetch(...); for performance.

The fix is on github in the master branch and will be included in 3.1.30.

Note: The speed of loops over $smarty->fetch() can be optimized further by creating a template object.

Code:
$smarty = new Smarty();

$loop = 0;
$speed = 100000;

$start = microtime(true);
$template = 'string: {if $price > 30}{math equation="x+y" x=$price y=20}{/if}';
$smarty->assign('price', '30');

$tpl = $smarty->create_template($template);

for ($i=0; $i<$speed; $i++) {
$tpl->fetch();
$loop++;
}


Original code on my slow system 0.000238
with template object 0.000160
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Mar 01, 2016 7:24 am    Post subject: Reply with quote

U.Tews wrote:
Note: The speed of loops over $smarty->fetch() can be optimized further by creating a template object.

Code:
$smarty = new Smarty();

$loop = 0;
$speed = 100000;

$start = microtime(true);
$template = 'string: {if $price > 30}{math equation="x+y" x=$price y=20}{/if}';
$smarty->assign('price', '30');

$tpl = $smarty->create_template($template);

for ($i=0; $i<$speed; $i++) {
$tpl->fetch();
$loop++;
}


Original code on my slow system 0.000238
with template object 0.000160

In which version of Smarty?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Mar 01, 2016 7:37 am    Post subject: Reply with quote

Apparently, this is ->createTemplate(), and doesn't really work up to 3.1.29.
Quote:
PHP Notice: Undefined index: a in ...\31-29\e1838377ea40bb61c740c6da09ca78be3f8ceba7_0.string.php on line 24
PHP Notice: Trying to get property of non-object in ...\31-29\e1838377ea40bb61c740c6da09ca78be3f8ceba7_0.string.php on line 24
PHP Notice: Undefined index: b in ...\31-29\e1838377ea40bb61c740c6da09ca78be3f8ceba7_0.string.php on line 24
PHP Notice: Trying to get property of non-object in ...\31-29\e1838377ea40bb61c740c6da09ca78be3f8ceba7_0.string.php on line 24
PHP Warning: math: function call x not allowed in ...\31-29\vendor\smarty\smarty\libs\plugins\function.math.php on line 53
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Mar 01, 2016 7:47 am    Post subject: Reply with quote

Code:
Smarty 3.1.27
String fetch  in 0.5550, or 0.0001 per run.
From template in 0.4850, or 0.0000 per run.

Smarty 3.1.28
String fetch  in 2.4700, or 0.0002 per run.
From template in 2.1300, or 0.0002 per run.

Smarty 3.1.29
String fetch  in 2.4625, or 0.0002 per run.
From template in 2.1275, or 0.0002 per run.

Smarty 3.1.30-dev/51
String fetch  in 0.5700, or 0.0001 per run.
From template in 0.4600, or 0.0000 per run.

Direct sting fetch is still slower in dev, but compiled template fetch is then summarily faster after latest fix.
Back to top
View user's profile Send private message
v.laios
Smarty Rookie


Joined: 15 Feb 2016
Posts: 8

PostPosted: Tue Mar 01, 2016 2:41 pm    Post subject: Reply with quote

I have tried the same benchmarks with templates. The new versions are same slower.

Smarty 3.27
Total: 2.2745139598846
One: 0.00011372569799423

Smarty 3.28
Total: 7.212886095047
One: 0.00036064430475235

Smarty 3.29
Total: 6.8465361595154
One: 0.00034232680797577
Back to top
View user's profile Send private message
v.laios
Smarty Rookie


Joined: 15 Feb 2016
Posts: 8

PostPosted: Tue Mar 01, 2016 2:44 pm    Post subject: Reply with quote

Excellent thank you very much for the fix!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Mar 01, 2016 6:01 pm    Post subject: Reply with quote

Yet, the ->createTemplate() is a very useful thing, if you need to, say, generate 50-60 SVG inserts for a page.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Mar 01, 2016 8:02 pm    Post subject: Reply with quote

Sorry, my code was incorrect. I had no parent link back to Smarty. So the template could not access the assigned variables,

Code:

$smarty = new Smarty();

$loop = 0;
$speed = 100000;

$start = microtime(true);
$template = 'string: {if $price > 30}{math equation="x+y" x=$price y=20}{/if}';
$smarty->assign('price', '30');

$tpl = $smarty->createTemplate($template, $smarty);

for ($i=0; $i<$speed; $i++) {
$tpl->fetch();
$loop++;
}
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP