in reply to Re: Re: Re: Circular buffer instead of shift/push
in thread Circular buffer instead of shift/push

Ok, I give up.
I certainly agree that replacing 'memory access with cheap operator'
is a good idea and obviously would be faster.
Here's some snips of benchmarks comparing the two:
@a=(1,2,3,0); $x=0; $m=4; timethese(-10,{ ixwheel=> q{ $x = $a[$x] } modby2x1=> q{ $x = (++$x) & $m} }); ixwheel: 434505.60/s modby2x1: 391856.16/s timethese(-10,{ ixwheel=> q{ $x=$a[$x] } modby2x2=> q{ $x++; $x &= $m }}); ixwheel: 433011.28/s modby2x2: 539474.07/s
Well, ok. For some reason the 2 statement version is much faster
than the 1 statement one (($x+1) was even slower than (++$x)!).
But now look what happens when they're used as an index:
timethese(-10,{ ixwheelix=> q{ $b[ $x=$a[$x] ] = $x } modby2x2ix=> q{ $b[$x++] = $x; $x &= $m }}); ixwheelix: 262030.67/s modby2x2ix: 220259.36/s
Again, this seemed to be the fastest of the variations.

But then, I'm using perl 5.05003 on a sparc solaris, YMMV.

???????????,

p

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Circular buffer instead of shift/push
by repson (Chaplain) on Jan 13, 2001 at 13:14 UTC
    Have you tested that code before benchmarking cause I can't the bitwise ands to produce more that a string of zeros. Anyway heres some more tests

    use Benchmark; my $m=4; my @a=(1,2,3,0); my $x=0; timethese(-10, { wheel => sub {$x = $a[$x]}, equaland => sub {++$x==$m and $x=0}, mod => sub {$x = ($x+1) % $m;}, trinary => sub {$x = ++$x==$m ? 0 : $x } } ); __END__ equaland: 23033.30/s trinary: 15301.14/s wheel: 12993.17/s mod: 12857.56/s
    So I didn't have your wheel on top of my list, unless I'm doing my benchmarking wrong again.
      'So I didn't have your wheel on top of my list'
      -- If you mean the output order, I had spelled it "ixwheel",
      (also, I may have patched Benchmark to do 'sort keys').

      For the more general response to your help, see below and slightly to the left.
      I replied at the same level as your reply in order to reply to both at once and so as not to march off the page completely.

      thank you,
      p
Re: Re: Re: Re: Re: Circular buffer instead of shift/push
by fundflow (Chaplain) on Jan 14, 2001 at 04:57 UTC
    As this is a very small test case, I'm not sure that it is meaningful. The idea of using an operator was to save some use of memory. In your tight loops, everything fits nicely in the L1 cache and so memory access might just be as fast.

    You have a bug in your code. Instead of 'and'ing with $m you need to use $m-1.

    Also, you as you used '$m' instead of '4' it might just be that Perl does not optimize it. You can either try to replace it with the number or add 'use constant'.

    Your analysis is interesting though.

    Cheers.