in reply to
How can I tell if a number is a power of 2?
Of course implementation is everything:
#!perl
use strict;
use warnings;
use Benchmark qw/cmpthese/;
sub tye {
my $x = shift;
0 == ( $x & ($x-1) ) ? 1 : 0;
}
sub tye2 {
my $x = shift;
!( $x & ($x-1) );
}
cmpthese( -5,
{
tye => q!tye(rand 1000000)!,
tye2 => q!tye2(rand 1000000)!,
}
);
__END__
Benchmark: running tye, tye2, each for at least 5 CPU seconds...
tye : 6 wallclock secs ( 5.07 usr + 0.00 sys = 5.07 CPU) @ 200612
+.27/s (n=1016703)
tye2: 6 wallclock secs ( 5.02 usr + 0.00 sys = 5.02 CPU) @ 225545
+.83/s (n=1131789)
Rate tye tye2
tye 200612/s -- -11%
tye2 225546/s 12% --