Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

cool off ... and let's do some benchmarks

by inq123 (Sexton)
on Mar 22, 2005 at 16:06 UTC ( [id://441519]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Calling a sub from a variable ?
in thread Calling a sub from a variable ?

Hi, ZlR,

First, cool off (I'm not trying to boss you around saying that). Even if the poster is an anonymous monk, the person's still trying to help, isn't it?

Additionally, although you follow Larry Wall's camel book well, you forgot to follow the last suggestion in "User Efficiency" section, Chapter 24 (page 603 in 3rd version), right?

Now back to business, let's do some benchmark:

Performance of eval - yeah, of course camel book's right, but it specifically said "avoid eval STRING inside a loop", yet you did not specify that's what you did in your post (and may I ask why you'd go against such suggestion in the first place?)

But, any comment should be taken in context. eval is in fact faster than &$ calls, if you use block instead of STRING (and coding eval block is possible in your situtation. Not pretty, but for performance it's worth it). That's due to the overhead of subroutines. Then again, in real life situation where your subroutine does any amount of real work, such difference is negligible. Why? Simply take a look at this benchmark using this code:

#!/usr/bin/perl use strict; use Benchmark; no strict 'refs'; my $sub = 'test'; my $t0 = new Benchmark; my ($in, $out) = ($ARGV[1], $ARGV[2]); if($ARGV[0] eq 'eval') { foreach (0..$out) { eval { my $i; foreach (0..$in) { $i++ } } } } if($ARGV[0] eq 'evalstr') { foreach (0..$out) { eval 'my $i; foreach (0..$in) { $i++ }'; } } elsif($ARGV[0] eq 'sub') { foreach (0..$out) { &$sub; } } elsif($ARGV[0] eq 'inline') { foreach (0..$out) { my $i; foreach (0..$in) { $i++ } } } my $t1 = new Benchmark; print "Method '$ARGV[0]' took:",timestr(timediff($t1, $t0)),"\n"; sub test { my $i; foreach (0..$in) { $i++ } }
Result:
inq123@perlmonks$ perl test.pl inline 1 1000000
Method 'inline' took: 3 wallclock secs ( 2.86 usr +  0.00 sys =  2.86 CPU)
inq123@perlmonks$ perl test.pl eval 1 1000000
Method 'eval' took: 3 wallclock secs ( 3.40 usr +  0.00 sys =  3.40 CPU)
inq123@perlmonks$ perl test.pl sub 1 1000000
Method 'sub' took: 4 wallclock secs ( 3.57 usr +  0.00 sys =  3.57 CPU)
inq123@perlmonks$ perl test.pl evalstr 1 1000000
Method 'evalstr' took:42 wallclock secs (41.25 usr +  0.00 sys = 41.25 CPU)

inq123@perlmonks$ perl test.pl inline 10 100000
Method 'inline' took: 0 wallclock secs ( 0.47 usr +  0.00 sys =  0.47 CPU)
inq123@perlmonks$ perl test.pl eval 10 100000
Method 'eval' took: 1 wallclock secs ( 0.53 usr +  0.00 sys =  0.53 CPU)
inq123@perlmonks$ perl test.pl sub 10 100000
Method 'sub' took: 0 wallclock secs ( 0.55 usr +  0.00 sys =  0.55 CPU)
inq123@perlmonks$ perl test.pl evalstr 10 100000
Method 'evalstr' took: 4 wallclock secs ( 4.30 usr +  0.00 sys =  4.30 CPU)

inq123@perlmonks$ perl test.pl inline 100 10000
Method 'inline' took: 1 wallclock secs ( 0.25 usr +  0.00 sys =  0.25 CPU)
inq123@perlmonks$ perl test.pl eval 100 10000
Method 'eval' took: 0 wallclock secs ( 0.24 usr +  0.00 sys =  0.24 CPU)
inq123@perlmonks$ perl test.pl sub 100 10000
Method 'sub' took: 1 wallclock secs ( 0.25 usr +  0.00 sys =  0.25 CPU)
inq123@perlmonks$ perl test.pl evalstr 100 10000
Method 'evalstr' took: 0 wallclock secs ( 0.62 usr +  0.00 sys =  0.62 CPU)
It's clear that eval BLOCK is faster than &$, and both are much faster than putting eval STRING in a loop (again proving the wisdom of camel book :) ). That's why I said eval does not suffer significant performance penalty (except when you use it in a way that penalizes it).

Now note that I'm just another guy trying to help out, so please don't flame me even if you don't like the post or the conclusion ... ;)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-16 23:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found