in reply to What would you do?

I know, I said efficiency didn't matter, but I couldn't help myself:
use strict; use warnings; use Benchmark qw(cmpthese); my @list = (0..100); cmpthese (-3, { c_style => sub { for (my $i = 0; $i <= $#list; $i += 2) { $list[$i] = 'hi'; } }, slice => sub { $_ = 'hi' for @list[map {$_ * 2} 0..$#list/2] }, modulo => sub { $_ % 2 or $list[$_] = 'hi' for 0..$#list; }, doubler => sub { $list[$_ * 2] = 'hi' for 0..@list/2; }, evilgrep => sub { @list[grep{!$_&1}0..$#list]= ('hi')x@list; } }); ### RESULTS ### evilgrep 8006/s -- -5% -17% -39% -41% modulo 8453/s 6% -- -12% -35% -37% slice 9630/s 20% 14% -- -26% -29% c_style 13041/s 63% 54% 35% -- -3% doubler 13509/s 69% 60% 40% 4% --

Replies are listed 'Best First'.
(tye)Re2: What would you do? (benchmarks...)
by tye (Sage) on Mar 17, 2001 at 03:33 UTC

    I modified "evilgrep" to use (@list/2) instead of @list since the whole purpose of these benchmarks was obviously to make me look stupid. ;)

    Rate evilgrep slice modulo grep c_style doubler evilgrep 4708/s -- -16% -20% -20% -45% -54% slice 5584/s 19% -- -5% -5% -34% -46% modulo 5865/s 25% 5% -- -0% -31% -43% grep 5893/s 25% 6% 0% -- -31% -43% c_style 8524/s 81% 53% 45% 45% -- -17% doubler 10320/s 119% 85% 76% 75% 21% --
    So I just wanted to note that I'm now in third place instead of last and that I did this with my wimpy office machine instead of my kick-arse home machine or my runs/second would have been much higher than yours as well! q-:

            - tye (but my friends don't call me)
      Well, I'm honor-bound to reply that your seeming algorithmic superiority is directly attributable to your pernicious use of bitwise operations. You also have a subtle precedence bug in evilgrep that is fixed in the code below, and invalidates all your results. Furthermore, all your benchmarks are belong to me:
      use Benchmark qw(cmpthese); my @list = (0..100); cmpthese (-3, { c_style => sub { for (my $i = 0; $i <= $#list; $i += 2) {$list[$i] += 'h1' } }, slice => sub { $_ = 'h2' for @list[map {$_ << 1} 0..$#list/2] }, modulo => sub { $_ & 1 or $list[$_] = 'h3' for 0..$#list }, doubler => sub { $list[$_ << 1] = 'h4' for 0..@list/2 }, evilgrep => sub { @list[grep{!($_&1)}0..$#list]= ('h5')x @list }, grep => sub { @list[grep{!($_&1)}0..$#list]= ('h6')x(@list/2 + 1 +) } }); ### RESULTS ### evilgrep 7467/s -- -19% -40% -42% -43% -65% grep 9244/s 24% -- -25% -28% -30% -57% c_style 12398/s 66% 34% -- -3% -6% -42% modulo 12811/s 72% 39% 3% -- -3% -40% slice 13211/s 77% 43% 7% 3% -- -38% doubler 21289/s 185% 130% 72% 66% 61% --
      ps. My system is currently underclocked, not to mention having slow timings, 4-way interleaving, and CAS2 disabled. If and when I reboot and tweak, I welcome you to bring it! :P

      pps. Assuming of course, my computer boots up...

         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print

        Well, at least you succeeded at making me look stupid this time. ;)

        For great justice!

                - tye (but my friends call me "Tye")