Hmm, perhaps. The way I was looking at it is that its a * k / b * k (which im not arguing is correct, as i dont know, merely explaining :-). Where if the k=1 when using eval and k=1.1 using a subref the ratio stays the same.
But the cost of calling a sub is independent of the size, or running time, of the body of the sub. Hence, you get (fixed) additional time for each of the clauses that contain a sub ref. And you pay that price *for each invocation of the sub*. When using a string, the code is evalled only once.

Here's a benchmark of a fairly trivial action, once done with a sub, and once done with an eval:

use strict; use warnings 'all'; use Benchmark 'cmpthese'; $::a = "foo-bar"; cmpthese -5 => { sub => sub {$::a =~ /([-])/}, str => '$::a =~ /([-])/', }; __END__ Benchmark: running str, sub for at least 5 CPU seconds... str: 5 wallclock secs ( 5.34 usr + 0.00 sys = 5.34 CPU) @ 39 +0154.68/s (n=2083426) sub: 5 wallclock secs ( 5.13 usr + 0.00 sys = 5.13 CPU) @ 28 +2139.18/s (n=1447374) Rate sub str sub 282139/s -- -28% str 390155/s 38% --
Here's the relevant code from Benchmark::runloop:
my ($subcode, $subref); if (ref $c eq 'CODE') { $subcode = "sub { for (1 .. $n) { local \$_; package $pack; &\ +$c; } }"; $subref = eval $subcode; } else { $subcode = "sub { for (1 .. $n) { local \$_; package $pack; $c +;} }"; $subref = _doeval($subcode); }
BTW, i assume
my $opts='-5 \\\\\\\\foo\\\\bar\\\\baz.exe';
is because your shell is converting \\ to \?
Yes. I could have used
my $opts=q!-5 '\\\\foo\\bar\\baz.exe'!;
too.

Abigail


In reply to Re: Unfortunately benchmarking things with $& isn't easy... by Abigail-II
in thread Obtaining server name from UNC path by blackadder

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.