Last active
June 26, 2017 15:30
-
-
Save renatocron/3121147dd82444e7572a4c11055977f5 to your computer and use it in GitHub Desktop.
Benchmarking Mojo::Template / Text::Xslate /Template with loops and condicionals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Mojo::Template; | |
use Text::Xslate; | |
use Template; | |
# some useful options (see below for full list) | |
my $config = { | |
EVAL_PERL => 0, # evaluate Perl code blocks | |
}; | |
# create Template object | |
my $template = Template->new($config); | |
use Benchmark qw/timethese cmpthese/; | |
my $ref = [ | |
map { ( { name => 'peach', color => 'white', number => $_, } ) } ( 0 .. 1000 ), | |
]; | |
my $tx = Text::Xslate->new; | |
my $mt = Mojo::Template->new; | |
my $result = timethese( | |
-5, | |
{ | |
mojo_template => sub { | |
$mt->render( ' | |
% my ($av) = @_; | |
% foreach my $item (@$av) { | |
<ul> | |
<li>name:<%= $item->{name} %></li> | |
<li>color:<%= $item->{color} %></li> | |
<% if ($item->{number} > 2) { %> | |
<li>number:<%= $item->{number} %></li> | |
<% } %> | |
</ul> | |
% } | |
', $ref ); | |
}, | |
text_xslate => sub { | |
$tx->render_string( | |
': for $av -> $item { | |
<ul> | |
<li>name:<: $item.name :></li> | |
<li>color:<: $item.color :></li> | |
: if $item.number > 2 { | |
<li>number:<: $item.number :></li> | |
: } | |
</ul> | |
: } ', { av => $ref } | |
); | |
}, | |
tt => sub { | |
my $out = ''; | |
$out = $template->process( | |
\' [% FOR item IN av %] | |
<ul> | |
<li>name:[% item.name %]</li> | |
<li>color:[% item.color %]</li> | |
[% IF item.number > 2 %] | |
<li>number:[% item.number %]</li> | |
[%END%] | |
</ul> | |
[% END %] ', { av => $ref }, \$out | |
); | |
}, | |
} | |
); | |
cmpthese($result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Benchmark: running mojo_template, text_xslate, tt for at least 5 CPU seconds... | |
mojo_template: 5 wallclock secs ( 5.35 usr + 0.00 sys = 5.35 CPU) @ 920.56/s (n=4925) | |
text_xslate: 5 wallclock secs ( 5.35 usr + 0.00 sys = 5.35 CPU) @ 526.36/s (n=2816) | |
tt: 5 wallclock secs ( 5.14 usr + 0.00 sys = 5.14 CPU) @ 84.63/s (n=435) | |
Rate tt text_xslate mojo_template | |
tt 84.6/s -- -84% -91% | |
text_xslate 526/s 522% -- -43% | |
mojo_template 921/s 988% 75% -- | |
# AMD Ryzen 7 1800X Eight-Core Processor @ 3.6 ghz + some boost probability, no OC | |
Template (2.27) | |
Text::Xslate (3.4.0) | |
Mojo::Template => (Mojolicious (7.33)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I also changed line 19 to
my $tx = Text::Xslate->new(type => 'text');
and results are similar.