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 ... ;)


In reply to cool off ... and let's do some benchmarks by inq123
in thread Calling a sub from a variable ? by ZlR

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, details, 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, summary, 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.