CGI-Ex-Template-XS

 view release on metacpan or  search on metacpan

samples/bench_template.pl  view on Meta::CPAN

### write out some file to be used later

my $fh;
my $bar_template = "$tt_cache_dir/bar.tt";
END { unlink $bar_template };
open($fh, ">$bar_template") || die "Couldn't open $bar_template: $!";
print $fh "BAR";
close $fh;

my $baz_template = "$tt_cache_dir/baz.tt";
END { unlink $baz_template };
open($fh, ">$baz_template") || die "Couldn't open $baz_template: $!";
print $fh "[% SET baz = 42 %][% baz %][% bing %]";
close $fh;

my $longer_template = "[% INCLUDE bar.tt %]"
    ."[% array.join('|') %]"
    .("123"x200)
    ."[% FOREACH a IN array %]foobar[% IF a == 'A' %][% INCLUDE baz.tt %][% END %]bazbing[% END %]"
    .("456"x200)
    ."[% IF foo ; bar ; ELSIF baz ; bing ; ELSE ; bong ; END %]"
    .("789"x200)
    ."[% IF foo ; bar ; ELSIF baz ; bing ; ELSE ; bong ; END %]"
    .("012"x200)
    ."[% IF foo ; bar ; ELSIF baz ; bing ; ELSE ; bong ; END %]"
    ."[% array.join('|') %]"
    ."[% PROCESS bar.tt %]";

my $hello2000 = "<html><head><title>[% title %]</title></head><body>
[% array = [ \"Hello\", \"World\", \"2000\", \"Hello\", \"World\", \"2000\" ] %]
[% sorted = array.sort %]
[% multi = [ sorted, sorted, sorted, sorted, sorted ] %]
<table>
[% FOREACH row = multi %]
  <tr bgcolor=\"[% loop.count % 2 ? 'gray' : 'white' %]\">
  [% FOREACH col = row %]
    <td align=\"center\"><font size=\"+1\">[% col %]</font></td>
  [% END %]
  </tr>
[% END %]
</table>
[% param = integer %]
[% FOREACH i = [ 1 .. 10 ] %]
  [% var = i + param %]"
  .("\n  [%var%] Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World <br/>"x20)."
[% END %]
</body></html>
";

###----------------------------------------------------------------###
### set a few globals that will be available in our subs
my $show_list = grep {$_ eq '--list'} @ARGV;
my $run_all   = grep {$_ eq '--all'}  @ARGV;
my @run = $run_all ? () : @ARGV;
my $str_ref;
my $filename;

### uncomment to run a specific test - otherwise all tests run
#@run = qw(07);

#                                                                         ### All percents are CGI::Ex::Template::XS vs TT2 with Stash::XS
#                                                                         ### (The percent that CET is faster than TT)
#                                                                               Existing object by string ref #
#                                                                      New object with CACHE_EXT set #        #
#                                                   New object each time (undef CACHE_SIZE) #        #        #
#                              This percent is compiled in memory (repeated calls) #        #        #        #
my $tests = {                                                             #        #        #        #        #
    '01_empty'     => "",                                                 #  268%  #  585%  #  318%  #  414%  # 23863.2/s #
    '02_var_sma'   => "[% one %]",                                        #  225%  #  563%  #  465%  #  466%  # 19051.2/s #
    '03_var_lar'   => "[% one %]"x100,                                    #  223%  #  405%  #  196%  #  396%  # 2508.9/s #
    '04_set_sma'   => "[% SET one = 2 %]",                                #  206%  #  489%  #  433%  #  372%  # 17831.8/s #
    '05_set_lar'   => "[% SET one = 2 %]"x100,                            #   88%  #  252%  #   68%  #  259%  # 1572.8/s #
    '06_set_range' => "[% SET one = [0..30] %]",                          #   68%  #  292%  #  268%  #  199%  # 9586.9/s #
    '07_chain_sm'  => "[% hash.a %]",                                     #  246%  #  602%  #  460%  #  504%  # 18270.1/s #
    '08_mixed_sma' => "".((" "x100)."[% one %]\n")x10,                    #  226%  #  546%  #  374%  #  521%  # 11274.8/s #
    '09_mixed_med' => "".((" "x10)."[% one %]\n")x100,                    #  227%  #  502%  #  266%  #  499%  # 2327.1/s #
    '10_str_sma'   => "".("[% \"".(" "x100)."\$one\" %]\n")x10,           #   32%  #  1434%  #  164%  #  1512%  # 4469.6/s #
    '11_str_lar'   => "".("[% \"".(" "x10)."\$one\" %]\n")x100,           #  -15%  #  354%  #   53%  #  350%  # 603.3/s #
    '12_num_lterl' => "[% 2 %]",                                          #  207%  #  558%  #  464%  #  426%  # 18975.7/s #
    '13_plus'      => "[% 1 + 2 %]",                                      #  139%  #  431%  #  375%  #  305%  # 14909.4/s #
    '14_chained'   => "[% c.d.0.hee.0 %]",                                #  222%  #  608%  #  439%  #  514%  # 17767.7/s #
    '15_chain_set' => "[% SET c.d.0.hee.0 = 2 %]",                        #  243%  #  498%  #  412%  #  416%  # 15385.6/s #
    '16_chain_lar' => "[% c.d.0.hee.0 %]"x100,                            #  211%  #  501%  #  133%  #  499%  # 1581.9/s #
    '17_chain_sl'  => "[% SET c.d.0.hee.0 = 2 %]"x100,                    #  460%  #  380%  #  206%  #  379%  # 958.1/s #
    '18_cplx_comp' => "[% t = 1 || 0 ? 0 : 1 || 2 ? 2 : 3 %][% t %]",     #  132%  #  271%  #  297%  #  199%  # 12497.7/s #
    '19_if_sim_t'  => "[% a=1 %][% IF a %]Two[% END %]",                  #  189%  #  462%  #  385%  #  370%  # 15692.7/s #
    '20_if_sim_f'  => "         [% IF a %]Two[% END %]",                  #  222%  #  573%  #  442%  #  482%  # 18290.7/s #
    '21_if_else'   => "[% IF a %]A[% ELSE %]B[% END %]",                  #  202%  #  529%  #  423%  #  424%  # 17274.5/s #
    '22_if_elsif'  => "[% IF a %]A[% ELSIF b %]B[% ELSE %]C[% END %]",    #  208%  #  494%  #  397%  #  423%  # 16425.9/s #
    '23_for_i_sml' => "[% FOREACH i = [0..10]   ; i ; END %]",            #   96%  #  286%  #  247%  #  216%  # 4238.6/s #
    '24_for_i_med' => "[% FOREACH i = [0..100]  ; i ; END %]",            #   60%  #  128%  #   99%  #  100%  # 714.7/s #
    '25_for_sml'   => "[% FOREACH [0..10]       ; i ; END %]",            #   83%  #  282%  #  230%  #  219%  # 3864.5/s #
    '26_for_med'   => "[% FOREACH [0..100]      ; i ; END %]",            #   59%  #  119%  #   95%  #   96%  # 660.6/s #
    '27_while'     => "[% f = 10 %][%WHILE f%][%f=f- 1%][%f%][% END %]",  #   75%  #  246%  #  159%  #  200%  # 2803.9/s #
    '28_whl_set_l' => "[% f = 10; WHILE (g=f) ; f = f - 1 ; f ; END %]",  #   64%  #  208%  #  138%  #  170%  # 2187.9/s #
    '29_whl_set_s' => "[% f = 1;  WHILE (g=f) ; f = f - 1 ; f ; END %]",  #  117%  #  330%  #  270%  #  268%  # 8598.6/s #
    '30_file_proc' => "[% PROCESS bar.tt %]",                             #  290%  #  540%  #  389%  #  492%  # 12828.6/s #
    '31_file_incl' => "[% INCLUDE baz.tt %]",                             #  217%  #  428%  #  317%  #  391%  # 8809.3/s #
    '32_process'   => "[% BLOCK foo %]Hi[% END %][% PROCESS foo %]",      #  189%  #  536%  #  425%  #  479%  # 12052.3/s #
    '33_include'   => "[% BLOCK foo %]Hi[% END %][% INCLUDE foo %]",      #  164%  #  509%  #  383%  #  436%  # 10283.2/s #
    '34_macro'     => "[% MACRO foo BLOCK %]Hi[% END %][% foo %]",        #  125%  #  399%  #  329%  #  320%  # 10132.7/s #
    '35_macro_arg' => "[% MACRO foo(n) BLOCK %]Hi[%n%][%END%][%foo(2)%]", #  123%  #  283%  #  325%  #  220%  # 9053.9/s #
    '36_macro_pro' => "[% MACRO foo PROCESS bar;BLOCK bar%]7[%END;foo%]", #  130%  #  426%  #  354%  #  365%  # 7684.1/s #
    '37_filter2'   => "[% n = 1 %][% n | repeat(2) %]",                   #  204%  #  401%  #  408%  #  314%  # 14354.7/s #
    '38_filter'    => "[% n = 1 %][% n FILTER repeat(2) %]",              #  148%  #  331%  #  348%  #  251%  # 11716.0/s #
    '39_fltr_name' => "[% n=1; n FILTER echo=repeat(2); n FILTER echo%]", #   98%  #  316%  #  284%  #  254%  # 8523.9/s #
    '40_constant'  => "[% constants.simple %]",                           #  209%  #  558%  #  448%  #  468%  # 19230.0/s #
    '41_perl'      => "[%one='ONE'%][% PERL %]print \"[%one%]\"[%END%]",  #   92%  #  415%  #  316%  #  355%  # 8329.8/s #
    '42_filtervar' => "[% 'hi' | \$filt %]",                              #  168%  #  522%  #  411%  #  426%  # 14287.9/s #
    '43_filteruri' => "[% ' ' | uri %]",                                  #  174%  #  572%  #  424%  #  490%  # 15174.3/s #
    '44_filterevl' => "[% foo | eval %]",                                 #  356%  #  581%  #  497%  #  524%  # 6285.5/s #
    '45_capture'   => "[% foo = BLOCK %]Hi[% END %][% foo %]",            #  161%  #  411%  #  347%  #  311%  # 14001.4/s #
    '46_complex'   => "$longer_template",                                 #  138%  #  346%  #  218%  #  305%  # 1866.3/s #
    '47_hello2000' => "$hello2000",                                       #  111%  #  270%  #  160%  #  260%  # 429.5/s #
    # overall                                                             #  167%  #  442%  #  311%  #  380%  #
};

### load the code representation
my $text = {};
seek DATA, 0, 0;
my $data = do { local $/ = undef; <DATA> };
foreach my $key (keys %$tests) {
    $data =~ m/(.*\Q$key\E.*)/ || next;
    $text->{$key} = $1;
}



( run in 0.505 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )