#!/usr/bin/env perl use strict; use warnings; use Template; use Text::Xslate; use Time::HiRes qw/tv_interval gettimeofday/; my $db_entities = { }; for (1 .. 100000) { $db_entities->{"entity_$_"} = { id => "$_", name => "name$_", level => $_ + 10, actiontype => "actiontype$_", }; } my @entity_ids = sort keys %$db_entities; warn "There are " . (scalar @entity_ids) . " entities in the db/hash\n"; for ( 1..3 ) { do_template('template with 2 vars', "entity [% entity.id %] - [% entity.name %]\n"); do_template('template with 3 vars', "entity [% entity.id %] - [% entity.level %] - [% entity.name %]\n"); do_template('template with 4 vars', "entity [% entity.id %] - [% entity.level %] - [% entity.actiontype %] - [% entity.name %]\n"); do_template('template with 4x2 vars', "entity [% entity.id %] - [% entity.level %] - [% entity.actiontype %] - [% entity.name %]\n" . "entity [% entity.id %] - [% entity.level %] - [% entity.actiontype %] - [% entity.name %]\n" ); } exit 0; sub timeblock { my ($text,$coderef) = @_; my $t0 = [gettimeofday]; my $res = $coderef->(); warn "$text: " . tv_interval($t0,[gettimeofday]) . "\n"; return $res } sub do_template { my $name = shift; my $template_line = shift; timeblock( "$name - TT - one template for all", sub { my $template = "[% FOR entity IN entities %]${template_line}[% END %]"; my $text = ''; my $tt = Template->new; $tt->process( \$template, { entities => [ map { $db_entities->{$_} } sort keys %$db_entities ] }, \$text ); $text; } ); timeblock( "$name - XSlate - one template for all", sub { my $template = "[% FOR entiey IN entities %]${template_line}[% END %]"; my $tx = Text::Xslate->new( syntax => 'TTerse' ); $tx->render_string( $template, { entities => [ map { $db_entities->{$_} } sort keys %$db_entities ] }); } ); } __END__ $ perl compare-templates.pl There are 100000 entities in the db/hash template with 2 vars - TT - one template for all: 1.000932 template with 2 vars - XSlate - one template for all: 0.305284 template with 3 vars - TT - one template for all: 1.291512 template with 3 vars - XSlate - one template for all: 0.292315 template with 4 vars - TT - one template for all: 1.581457 template with 4 vars - XSlate - one template for all: 0.347965 template with 4x2 vars - TT - one template for all: 2.903464 template with 4x2 vars - XSlate - one template for all: 0.549997 template with 2 vars - TT - one template for all: 0.993165 template with 2 vars - XSlate - one template for all: 0.266156 template with 3 vars - TT - one template for all: 1.286004 template with 3 vars - XSlate - one template for all: 0.30927 template with 4 vars - TT - one template for all: 1.571441 template with 4 vars - XSlate - one template for all: 0.36543 template with 4x2 vars - TT - one template for all: 2.81884 template with 4x2 vars - XSlate - one template for all: 0.513809 template with 2 vars - TT - one template for all: 0.948029 template with 2 vars - XSlate - one template for all: 0.256589 template with 3 vars - TT - one template for all: 1.268067 template with 3 vars - XSlate - one template for all: 0.290824 template with 4 vars - TT - one template for all: 1.562011 template with 4 vars - XSlate - one template for all: 0.328078 template with 4x2 vars - TT - one template for all: 2.873736 template with 4x2 vars - XSlate - one template for all: 0.548852