Just be careful to create your data as bitstrings in the first place. If you create arrays and then turn them into bitstrings to do the comparison, then it is not that fast:
Result:use strict; use warnings; use Benchmark 'cmpthese'; sub create { map {rand() < $_[1] ? 1 : 0} 1..$_[0] } sub compare2a { # first find 1s in x, then check in ys my $x = shift; my $n = shift; my @nxs = grep { $x->[$_] } 0..$n-1; return map { scalar grep {$_} @{$_}[@nxs] } @_; } sub compare4 { # bitstrings my $x = shift; $x = pack 'b*', join '', @$x; return map { unpack '%32b*', ( $x & pack 'b*', join'',@$_ ) } @_; } my $n = 15000; my $p = 0.005; my $ny = 10; my @x = create $n, $p; my @ys = map { [ create $n, $p ] } 1..$ny; my @r2a = compare2a \@x, $n, @ys; my @r4 = compare4 \@x, @ys; print "compare2a: @r2a\n"; print "compare4: @r4\n"; cmpthese( -5, { compare2a => sub{ compare2a \@x, $n, @ys }, compare4 => sub{ compare4 \@x, @ys }, } );
Rate compare4 compare2a compare4 246/s -- -55% compare2a 543/s 120% --
In reply to Re^3: Comparing two arrays
by hdb
in thread Comparing two arrays
by baxy77bax
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |