in reply to Re: cannot get Benchmark to work
in thread cannot get Benchmark to work

Another approach (usually recommended with Benchark) is to provide a string to eval rather than a subroutine reference
(Bold is mine). I dropped into the Benchmark documentation, and I indeed found:
a code reference will show a slightly slower execution time than the equivalent eval'd string
I only wonder... why?

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

Replies are listed 'Best First'.
Re^3: cannot get Benchmark to work
by fishbot_v2 (Chaplain) on Jul 04, 2005 at 15:05 UTC

    It is because Benchmark evals the code either way. It runs the code in the calling package, so if you give it a string to eval, then it just concatenates the package switching wrapper around the code, and evals it. If you give it a code ref, it just wraps the string "&$ref()" in a similar fashion.

    Either way, you suffer the same eval STR overhead. In the subref case, you also suffer the cost of a function call.

Re^3: cannot get Benchmark to work
by Anonymous Monk on Jul 05, 2005 at 11:51 UTC
    a code reference will show a slightly slower execution time than the equivalent eval'd string
    I only wonder... why?
    Because if you pass it a code reference, your benchmark needs to call a subroutine on each iteration. If you run your benchmark 10000 times, you will be calling 10000 subroutines, that is, you create 10000 blocks, and break them down 10000 times (think ref counting overhead), all which have nothing to do with the thing you are benchmarking.