in reply to cannot get Benchmark to work

You can't write \&a(\@foo) - you can take a reference to a subroutine like \&a, but then you can't specify parameters.

One solution is to create an anonymous subroutine that does the call:

a => sub { a(\@foo) }, b => sub { b(\@foo) },

Another approach (usually recommended with Benchark) is to provide a string to eval rather than a subroutine reference:

a => q{ a(\@foo) }, b => q{ b(\@foo) },
.. but in that case you need to be careful with variables: your my() variables won't be visible to Benchmark when it evals these, so you'd need to declare @foo in this case as a package variable:
our @foo = 1..10;

Hugo

Replies are listed 'Best First'.
Re^2: cannot get Benchmark to work
by polettix (Vicar) on Jul 04, 2005 at 13:34 UTC
    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.

      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.

      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.