But you are not measuring what you think you are! Once again, the high
number of iterations per second Benchmark is reporting should ring many
bells and whistles. Do you really think your computer is able to do more
than 3 billion modulus operations per second? Because if you believe
your Benchmark results, it's grepping through a 1000001 element array
315978.92 times a second.
Of course it isn't. You have my @array, which makes that
@array is only visible from the file. And not from an
eval that's happening in Benchmark.pm (that
would defeat all the purposes of my, wouldn't it?),
Changing the last lines to:
our @array = (0..1000000);
timethese(-10, {
'Step 1' => 'step1(500000, @::array)',
'Step 2' => 'step2(500000, @::array)',
'Step 3' => 'step3(500000, @::array)'
});
we get far more sensible results:
Benchmark: running Step 1, Step 2, Step 3 for at least 10 CPU seconds.
+..
Step 1: 10 wallclock secs (10.17 usr + 0.00 sys = 10.17 CPU) @ 5.1
+1/s (n=52)
Step 2: 10 wallclock secs (10.00 usr + 0.00 sys = 10.00 CPU) @ 5.1
+0/s (n=51)
Step 3: 11 wallclock secs (10.72 usr + 0.00 sys = 10.72 CPU) @ 0.4
+7/s (n=5)
Two things can be learned. First, don't blindly use my.
Think whether it's appropriate. Second, always distrust your code
if Benchmark returns many iterations per second - especially if you
do lots of work during an iteration.
Abigail
|