Skip to content

Instantly share code, notes, and snippets.

@gfx
Created April 15, 2010 05:12
Show Gist options
  • Select an option

  • Save gfx/366701 to your computer and use it in GitHub Desktop.

Select an option

Save gfx/366701 to your computer and use it in GitHub Desktop.
#!perl -w
use strict;
use Text::Xslate::Compiler;
use Text::ClearSilver;
use Text::MicroTemplate;
use Benchmark qw(:all);
use Config; printf "Perl/%vd %s\n", $^V, $Config{archname};
my $n = shift(@ARGV) || 100;
my $x = Text::Xslate::Compiler->new()
->compile_str("Hello, <?= \$lang ?> world!\n" x $n);
my $tcs = Text::ClearSilver->new();
my $mt = Text::MicroTemplate::build_mt(
"Hello, <?= \$_[0]->{lang} ?> world!\n" x $n
);
my $tcs_tmpl = qq{Hello, <?cs var:lang ?> world!\n} x $n;
my $subst_tmpl = qq{Hello, %lang% world!\n} x $n;
my $vars = {
lang => 'Template',
};
$x->render($vars) eq $mt->($vars) or die $x->render($vars);
# suppose PSGI response body
cmpthese -1 => {
xslate => sub {
my $body = [$x->render($vars)];
return;
},
clearsilver => sub{
my $body = [];
$tcs->process(\$tcs_tmpl, $vars, \$body->[0]);
return;
},
mt => sub {
my $body = [$mt->($vars)];
return;
},
's///g' => sub {
my $body = [$subst_tmpl];
$body->[0] =~ s/%(\w+)%/$vars->{$1}/g;
return;
},
};
$ perl -Mblib benchmark/interpolate.pl 100
Perl/5.10.1 i686-linux
Rate clearsilver mt s///g xslate
clearsilver 5120/s -- -29% -29% -76%
mt 7177/s 40% -- -0% -66%
s///g 7178/s 40% 0% -- -66%
xslate 20958/s 309% 192% 192% --
$ perl -Mblib benchmark/interpolate.pl 1000
Perl/5.10.1 i686-linux
Rate clearsilver mt s///g xslate
clearsilver 560/s -- -17% -26% -75%
mt 673/s 20% -- -11% -70%
s///g 757/s 35% 12% -- -66%
xslate 2221/s 297% 230% 193% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment