in reply to shift v. indexing into an array

Hello, I tried to benchmark those constructs like this:
use Benchmark qw(cmpthese); use 5.010; cmpthese(-2, { shift => sub { my @array = (1..10)x10000; my $item = shift @array; }, index => sub { my @array = (1..10)x10000; my $item = $array[0]; }, }); cmpthese(-2, { inside => sub { my $x = 0; while ($x < 5) { my $val = $x + 2; #repeatedly declaring a new variable? say $val; $x++; } }, outside => sub { my ($x, $val); $x = 0; while ($x < 5) { $val = $x + 2; #same old variable every time say $val; $x++; } }, });
For 1) it shows shift is slightly faster (it actually just moves pointer to start of the array, no moving occur).
Rate index shift index 62.6/s -- -1% shift 63.1/s 1% --
For 2) after filtering all printed output it seems declaring my inside loop makes it slightly slower. I would say inside declaration is cleaner and speedup is not worth avoiding it.
Rate inside outside inside 132994/s -- -9% outside 146050/s 10% --
All measured on Activestate perl 5.10 on Win32 ... YMMV

-- regards, Roman

Replies are listed 'Best First'.
Re^2: shift v. indexing into an array
by 7stud (Deacon) on Nov 15, 2009 at 16:01 UTC

    Hi,

    Thanks for the benchmarks. The shift results surprise me, but the my declaration results are what I would expect.

    Is there a "setup" option for benchmarking? For instance, allowing you to create the big array before the benchmarking begins.

      The benchmarking works by running the subroutines multiple times (the number is specified by first parameter of cmpthese). It is possible to move initialization outside as far as you don't modify them, which is not the case of pop nor shift.

      -- Roman

Re^2: shift v. indexing into an array
by dsheroh (Monsignor) on Nov 16, 2009 at 12:48 UTC
    For 1) it shows shift is slightly faster

    The difference is so small that I'd be more inclined to write this off as statistical noise and/or a CPU-specific characteristic and consider them identical.

    For 2) after filtering all printed output it seems declaring my inside loop makes it slightly slower. I would say inside declaration is cleaner and speedup is not worth avoiding it.

    Your conclusion is correct and the first sentence is why. In real code, you're going to be doing more in your loop than just declaring a variable, so a 10% difference in the time required to do that declaration will quickly fade into insignificance. Better to keep the declaration close to where the variable is first used to make the source clearer and eliminate any chance of an old value persisting from one iteration into the next.