use strict; use warnings; use Benchmark qw(cmpthese); use Types::Standard -types; use Template::Compiled; use Template; my $RENDER_COUNT = 1; my %data = ( name => 'Alice & Bob', age => 45.1 ); sub template_compiled { my $template = Template::Compiled->new( signature => [ name => Str, age => Int->plus_coercions(Num, q{ int $_ }), ], template => '

Hi . You are years old.

', escape => 'html', ); my $sub = $template->sub; return $sub->(\%data) if $RENDER_COUNT == 1; $sub->(\%data) for 1 .. $RENDER_COUNT; } sub template_toolkit { my $tt = Template->new; my $template = q{[% USE Math %]

Hi [% name|html %]. You are [% Math.int(age)|html %] years old.

}; my $out = ''; return $tt->process(\$template, \%data, \$out) && $out if $RENDER_COUNT == 1; $out = '' || $tt->process(\$template, \%data, \$out) for 1 .. $RENDER_COUNT; } for my $i (1, 10, 100) { $RENDER_COUNT = $i; print "\$RENDER_COUNT = $RENDER_COUNT\n"; cmpthese(-1, { TemplateToolkit => \&template_toolkit, TemplateCompiled => \&template_compiled, }); print "\n"; } __END__ $RENDER_COUNT = 1 Rate TemplateCompiled TemplateToolkit TemplateCompiled 517/s -- -47% TemplateToolkit 984/s 90% -- $RENDER_COUNT = 10 Rate TemplateToolkit TemplateCompiled TemplateToolkit 93.3/s -- -76% TemplateCompiled 394/s 322% -- $RENDER_COUNT = 100 Rate TemplateToolkit TemplateCompiled TemplateToolkit 9.62/s -- -95% TemplateCompiled 204/s 2018% --