in reply to Re: Rotating an array
in thread Rotating an array

Silly and slow
#!/usr/bin/perl -- use Benchmark qw( cmpthese ); @ARGV = ( 3,4,5,6 ) unless @ARGV; # 7 is too much for me :) for my $pow ( @ARGV ){ our $max = 10 ** $pow ; our $index = $max/10; print "\npow = $pow max = $max index = $index \$] = $]\n +"; cmpthese( -3, { pushShift => \&pushShift, slice => \&slice, pushSplice => \&pushSplice, splice => \&SPLICE, } ); } sub pushShift { my @arr = ( 1 .. $max ); push @arr,(shift @arr) for 1 .. $index; undef @arr; return; } sub slice { my @arr = ( 1 .. $max ); @arr=(@arr[$index..$#arr],@arr[0..($index-1)]); undef @arr; return; } sub pushSplice { my @arr = ( 1 .. $max ); push @arr, splice @arr,0,$index; undef @arr; return; } sub SPLICE { my @arr = ( 1 .. $max ); splice @arr,@arr,$index, splice @arr,0,$index; undef @arr; return; } __END__ pow = 3 max = 1000 index = 100 $] = 5.012002 Rate slice pushShift splice pushSplice slice 2422/s -- -53% -57% -60% pushShift 5144/s 112% -- -9% -15% splice 5659/s 134% 10% -- -6% pushSplice 6038/s 149% 17% 7% -- pow = 4 max = 10000 index = 1000 $] = 5.012002 Rate slice pushShift splice pushSplice slice 229/s -- -49% -53% -57% pushShift 446/s 95% -- -8% -17% splice 484/s 111% 8% -- -10% pushSplice 536/s 134% 20% 11% -- pow = 5 max = 100000 index = 10000 $] = 5.012002 Rate slice pushShift splice pushSplice slice 20.6/s -- -49% -55% -58% pushShift 40.3/s 95% -- -12% -18% splice 45.7/s 122% 13% -- -7% pushSplice 49.0/s 137% 22% 7% -- pow = 6 max = 1000000 index = 100000 $] = 5.012002 Rate slice pushShift splice pushSplice slice 2.05/s -- -53% -54% -59% pushShift 4.31/s 111% -- -3% -13% splice 4.46/s 118% 3% -- -10% pushSplice 4.98/s 143% 15% 12% --

Replies are listed 'Best First'.
Re^3: Rotating an array
by BrowserUk (Patriarch) on Jun 17, 2011 at 12:40 UTC

    What do you think you are comparing here? All of those move the original data, so they are all slow.


    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.
      What do you think you are comparing here?

      Nothing important, different ways to move

      All of those move the original data, so they are all slow.

      Acknowledged