Bit::Vector as you might guess isn't any better...
use strict;
use warnings;
use Time::HiRes qw[ time ];
use Bit::Vector;
my @lookup; $#lookup = 0x3ffff;
$lookup[ $_ ] = [ ( $_ & 0x3f000 ) >> 12, ( $_ & 0xfc0 ) >> 6, $_ & 0x
+3f ]
for 0 .. 0x3ffff;
my( @nxt, @mid, @bot );
$nxt[ $_ ] = ( $_ & 0x3f000 ) >> 12,
$mid[ $_ ] = ( $_ & 0xfc0 ) >> 6,
$bot[ $_ ] = $_ & 0x3f
for 0 .. 0x3ffff;
sub stuff{
# print "@_";
}
our $ITERS //= 10e6;
my $n = 0x80061861;
my $start = time;
for ( 1 .. $ITERS ) {
stuff(
( $n & 0xffc00000 ) >> 18,
( $n & 0x0003f000 ) >> 12,
( $n & 0x00000fc0 ) >> 6,
( $n & 0x0000003f )
);
}
printf " Shift&and took: %.12f seconds\n", ( time() - $start ) / $ITE
+RS;
$start = time;
for ( 1 .. $ITERS ) {
stuff( $n >> 18, @{ $lookup[ $n & 0x3ffff ] } );
}
printf " Lookup took: %.12f seconds\n", ( time() - $start ) / $ITE
+RS;
$start = time;
for ( 1 .. $ITERS ) {
my $i = $n & 0x3ffff;
stuff( $n >> 18, $nxt[ $i ], $mid[ $i ], $bot[ $i ] );
}
printf " Lookup2 took: %.12f seconds\n", ( time() - $start ) / $ITE
+RS;
$start = time;
for ( 1 .. $ITERS ) {
stuff( unpack 'vccc', join'',map{ pack'b*', $_ } unpack 'a14a6a6a6
+', unpack 'B32', pack 'N', $n );
}
printf " (un)pack took: %.12f seconds\n", ( time() - $start ) / $ITE
+RS;
$n="$n";
my $vector;
$start = time;
for (1 .. $ITERS ) {
$vector = Bit::Vector->new_Hex(32, $n);
stuff($vector->Chunk_Read(14, 18),
$vector->Chunk_Read( 6, 12),
$vector->Chunk_Read( 6, 6),
$vector->Chunk_Read( 6, 0)
);
}
printf "Bit::Vector took: %.12f seconds\n", ( time() - $start ) / $ITE
+RS;
__END__
C:\test>perl bv.pl
Shift&and took: 0.000000551174 seconds
Lookup took: 0.000000272709 seconds
Lookup2 took: 0.000000335823 seconds
(un)pack took: 0.000003393702 seconds
Bit::Vector took: 0.000005372898 seconds