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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.