an ord() and array lookup is one more operation than a hash lookup.
It depends upon how you break up the bitvector into its characters.
If you use unpack'C*', then there is no need to use ord. And as unpack is faster than split, using an array can work out 3 times faster than a hash.
And you can take that one step further. By using a lookup array twice the size and unpack'S*', you halve the number of lookups and get close to 8 times faster.
#! perl -slw
use strict;
use Data::Dump qw[ pp ];
use Benchmark qw[ cmpthese ];
my %lookup = map{
chr( $_ ) => unpack '%8b*', chr;
} 0 .. 255;
sub lookupHash {
my $n = 0;
$n += $lookup{ $_ } for split'', $_[ 0 ];
return $n;
}
my @lookupC = map unpack( '%8b*', chr ), 0 .. 255;
sub lookupArrayC {
my $n = 0;
$n += $lookupC[ $_ ] for unpack 'C*', $_[ 0 ];
return $n;
}
my @lookupS = map unpack( '%16b*', pack 'S', $_ ), 0 .. 65535;
sub lookupArrayS {
my $n = 0;
$n += $lookupS[ $_ ] for unpack 'S*', $_[ 0 ];
return $n;
}
our $bitvector = pack 'C*', ( 0 .. 255 ) x 10;
cmpthese -1, {
hash => q[ our $bitvector; my $n = lookupHash( $bitvector );
+],
arrayC => q[ our $bitvector; my $n = lookupArrayC( $bitvector );
+],
arrayS => q[ our $bitvector; my $n = lookupArrayS( $bitvector );
+],
};
__END__
C:\test>junk
Rate hash arrayC arrayS
hash 197/s -- -79% -89%
arrayC 930/s 373% -- -46%
arrayS 1717/s 773% 85% --
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.
|