in reply to Loops loosing Performance

For speed, I'd suggest:

sub test6 { my $n = $_[0] & $_[1]; my $count = 0; ++$count, $n &= $n - 1 while $n; $count; }

For beauty I'd suggest hiding that behind a suitable function name - it costs an extra function call, but you can probably lose that in the way you call it when not benchmarking:

sub countbits { my($n, $count) = ($_[0], 0); ++$count, $n &= $n - 1 while $n; return $count; } sub test7 { my($iDays, $iCmp) = @_; return countbits($iDays & $iCmp); }

You might even want to add some comments. :)

Benchmarks:

Rate Test4 Test5 Test7 Test6 Test4 106192/s -- -38% -40% -57% Test5 172463/s 62% -- -3% -31% Test7 176987/s 67% 3% -- -29% Test6 248686/s 134% 44% 41% --

Hugo

Replies are listed 'Best First'.
Re^2: Loops loosing Performance
by PerlingTheUK (Hermit) on Jun 10, 2005 at 06:36 UTC
    Thank you, test6 is what I was looking for. Quick but not a line for each bit of byte.

    Cheers,
    PerlingTheUK