my @bits_in = map $bits_in{chr($_ & 0xFF)} + $bits_in{chr($_ >> 8)}, (0..65535) ; # Use vec() and @bits_in -- faster than ord(substr(..)) sub loop_count_va { # "lpva" my ( $bits ) = @_; my $count; $count += $bits_in[vec($bits, $_, 8)] for 0 .. length( $bits ) - 1; return $count; } # As "lpva" but 16 bits at a time sub loop_count_VA { # "lpVA" my ( $bits ) = @_; my $count; $count += $bits_in[vec($bits, $_, 16)] for 0 .. (length( $bits ) - 1) >> 1 ; return $count; } # The other other scheme sub unpack_count { # "unpk" my ( $bits ) = @_ ; my $count = unpack("%32b*", $bits); if ($count != $COUNT) { die "unpack_count $count != $COUNT" ; } ; return $count; }