in reply to Re^6: Removing AUTOLOAD from CGI.pm
in thread Removing AUTOLOAD from CGI.pm
Here a simplified example which showed that installing subs with closures¹ is 5 times faster than eval.
That's less dramatic than I thought, but eval performance also heavily depends on the length of code.
So adding 10 no-ops already made eval 10 times slower while closure didn't change.
use strict; use warnings; use Time::HiRes qw/time/; BEGIN { $\="\n"; my $count=1000; sub noop { }; my $start=time; sub tag_gen { my ($tag)=@_; sub { noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); "<$tag>",@_,"</$tag>"; } } { no strict 'refs'; for my $x (1..$count) { my $tag="h$x"; *{$tag}=tag_gen($tag); } } print "Closure: ",time-$start; $start = time; for my $x (1..$count) { my $tag="H$x"; eval sprintf <<'__CODE__', $tag; sub %1$s { noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); noop("Ipse Lorum"); "<%1$s>",@_,"</%1$s>"; } __CODE__ } print "Eval: ",time-$start; } print h1 h2 "closure"; print H1 H2 "eval";
output:
Closure: 0.0279860496520996 Eval: 0.343567132949829 <h1><h2>closure</h2></h1> <H1><H2>eval</H2></H1>
One has to keep in mind that 15-20 years ago machines were factor 1000 slower, i.e. auto loading was relevant.
fixed bug with no-ops.
¹) currying to be precise
|
|---|