#!perl
use 5.006;
# file: pack.t
use strict;
use warnings;
use Benchmark qw(:all) ;
use Data::Dumper;
use List::Util qw(pairs);
my @diffs = map { $_,$_; } (0..49);
#print '@diffs: ',Dumper(\@diffs),"\n";
my $packed0 = pack('V*',@diffs);
my $a = [];
#print 'len: ',length( $packed0),"\n";
my $packed = $packed0;
while (length( $packed)) {
push @$a,[unpack ('VV', substr( $packed, 0, 8, ''))];
}
#print Dumper($a);
timethese( 50_000, {
'unpack while' => sub {
my $packed = $packed0;
while (length( $packed)) {
my ($x,$y)= unpack ('VV', substr( $packed, 0, 8, ''));
}
},
'unpack while single' => sub {
my $packed = $packed0;
while (length( $packed)) {
my $x = unpack ('V', substr( $packed, 0, 4, ''));
}
},
'unpack while push' => sub {
my $packed = $packed0;
$a = [];
while (length( $packed)) {
push @$a,[unpack ('VV', substr( $packed, 0, 8, ''))];
}
},
'unpack for' => sub {
for ( my $i = 0;$i < length( $packed0)-1; $i += 8 ) {
my ($x,$y)= unpack ('VV', substr( $packed0, $i, 8));
}
},
'unpack for push' => sub {
$a = [];
for ( my $i = 0;$i < length( $packed0)-1; $i += 8 ) {
push @$a,[unpack ('VV', substr( $packed0, $i, 8))];
}
},
});
########
$ perl pack.t
Benchmark: timing 50000 iterations of unpack for, unpack for push, unp
+ack while, unpack while push, unpack while single...
unpack for: 1 wallclock secs ( 1.01 usr + 0.00 sys = 1.01 CPU) @ 49
+504.95/s (n=50000)
unpack for push: 2 wallclock secs ( 1.84 usr + 0.00 sys = 1.84 CPU)
+ @ 27173.91/s (n=50000)
unpack while: 1 wallclock secs ( 0.84 usr + 0.00 sys = 0.84 CPU) @
+59523.81/s (n=50000)
unpack while push: 2 wallclock secs ( 1.75 usr + 0.00 sys = 1.75 CP
+U) @ 28571.43/s (n=50000)
unpack while single: 1 wallclock secs ( 1.28 usr + 0.00 sys = 1.28
+CPU) @ 39062.50/s (n=50000)
With bitmaps it would be an array of 2 scalars (2 x 64-bit IVs, 53 bits used in the original test case). |