Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^7: Removing AUTOLOAD from CGI.pm (closure vs eval)

by LanX (Saint)
on Feb 24, 2015 at 14:19 UTC ( [id://1117657]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Removing AUTOLOAD from CGI.pm
in thread Removing AUTOLOAD from CGI.pm

> I'm sure compilation speed shouldn't matter when generating closures. (I will add a benchmark later this day)

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.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)

PS: Je suis Charlie!

update

fixed bug with no-ops.

¹) currying to be precise

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1117657]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2024-04-18 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found