#!/usr/bin/env perl use strict; use warnings; use Template; 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('SWITCH with multiple statements for same var', '[% SWITCH entity.id; CASE 1 %]ONE[% CASE 2 %]TWO[% CASE 3 %]THREE[% CASE DEFAULT %]DEFAULT[% END %]' ); do_template('IF with multiple statements for same var', '[% IF entity.id == 1 %]ONE[% ELSIF entity.id == 2 %]TWO[% ELSIF entity.id == 3 %]THREE[% ELSE %]DEFAULT[% END %]' ); } 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; } ); } __END__ There are 100000 entities in the db/hash SWITCH with multiple statements for same var - TT - one template for all: 1.426351 IF with multiple statements for same var - TT - one template for all: 1.131126 SWITCH with multiple statements for same var - TT - one template for all: 1.43776 IF with multiple statements for same var - TT - one template for all: 1.250805 SWITCH with multiple statements for same var - TT - one template for all: 1.35122 IF with multiple statements for same var - TT - one template for all: 1.089041