Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Recently a meditation about templates took place and speed was stressed. I read that being at the end perl's coderef your templates must be fast: you tried some bench with big templates too?

Sure. Here's a comparison with Template::Toolkit:

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 => '<p>Hi <?= $name ?>. You are <?= $age ?> years old +.</p>', 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 %]<p>Hi [% name|html %]. You are [% M +ath.int(age)|html %] years old.</p>}; 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% --

Template::Toolkit is about twice as fast in the case where the template is used once only. If the template is compiled once and then re-used ten times, then Template::Compiled is about four times the speed. If it's used 100 times, then Template::Compiled is more than 20 times the speed.

It's not hard to figure out which would be the winner in the case where you needed to send an HTML e-mail to 100,000 customers.

It's also worth noting that in all these examples, Template::Compiled is doing more for you than Template::Toolkit is. For example, it is sanity checking arguments. If you do:

$data{name} = [];

Then Template::Toolkit will happily print:

<p>Hi ARRAY(0x1afa178). You are 45 years old.</p>

Template::Compiled will throw an exception because you said name is a Str.

You say module that allows you to embed Perl in your templates but is not the tendence to extrapolate templates from source code?

Any non-trivial template implementation will support things like conditional blocks and loop structures. Most will invent their own syntax for it, like in Template::Toolkit:

[% IF atrisk %] Danger Will Robinson [% END %]

Template::Compiled just allows you to use Perl in the template:

<? if ($atrisk) { ?> Danger Will Robinson <? } ?>

Hopefully this addresses your questions.


In reply to Re^2: Because CPAN Needs More Templating Modules by tobyink
in thread Because CPAN Needs More Templating Modules by tobyink

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [Corion]: A good daystart to everbody! I think martos snow from Monday(?) has arrived here today as storm+rain ;)

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (5)
    As of 2018-01-18 09:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How did you see in the new year?










      Results (208 votes). Check out past polls.

      Notices?