in reply to Re^2: Inconsistent Results with Benchmark
in thread Inconsistent Results with Benchmark
I don't fully understand how q and square brackets work in your code, even after just looking up some documentation.
Benchmark will accept a string containing a piece of code, where you normally supply a subroutine. From the synopsis:
# Use Perl code in strings... timethese($count, { 'Name1' => '...code1...', 'Name2' => '...code2...', }); # ... or use subroutine references. timethese($count, { 'Name1' => sub { ...code1... }, 'Name2' => sub { ...code2... }, }); # cmpthese can be used both ways as well cmpthese($count, { 'Name1' => '...code1...', 'Name2' => '...code2...', }); cmpthese($count, { 'Name1' => sub { ...code1... }, 'Name2' => sub { ...code2... }, });
That's what my example did.
What actually happens under the covers (greatly simplified) is that a call to the code reference (subroutine) that you supply to Benchmark is eval'd into another subroutine within the package that wraps that call in a loop:
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); }
As you can see, if what you supply is a string rather than a code ref, that string is eval'd into that extra level of subroutine instead.
From the Benchmark docs:
CAVEATSComparing eval'd strings with code references will give you inaccurate results: a code reference will show a slightly slower execution time than the equivalent eval'd string.
So either use code refs, or strings, but do not mix the two. (Though in the case of our dummy sub that just forces preallocation of memory, it doesn't matter as it isn't a part of the timing.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Inconsistent Results with Benchmark
by benwills (Sexton) on Dec 08, 2014 at 07:12 UTC |