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:

CAVEATS

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


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: Inconsistent Results with Benchmark
by benwills (Sexton) on Dec 08, 2014 at 07:12 UTC

    Ah, that makes sense now. I knew several of those pieces, but didn't put them together like you just did.

    I saw that q returns a string, but was confused as to what Benchmark would do with that. But I also knew that Benchmark eval'd code in a loop. I just didn't put it all together.

    Thanks for taking the time to explain that.