more useful options PerlMonks

Re^3: Comparing two arrays

by hdb (Prior)
 on Dec 15, 2013 at 14:03 UTC ( #1067234=note: print w/replies, xml ) Need Help??

in reply to Re^2: Comparing two arrays

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:

```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 },
}
);
Result:
```           Rate  compare4 compare2a
compare4  246/s        --      -55%
compare2a 543/s      120%        --

Replies are listed 'Best First'.
Re^4: Comparing two arrays
by BrowserUk (Pope) on Dec 30, 2013 at 22:29 UTC
If you create arrays and then turn them into bitstrings [ everytime ] to do the comparison, then it is not that fast:

No shit Sherlock :)

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Create A New User
Node Status?
node history
Node Type: note [id://1067234]
help
Chatterbox?
 [Corion]: My favourite Bob Fosse choreography, basically redone in Austin Powers I think [Corion]: Only now I realize that both pieces have the same melody, Big Spender and The Frug

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2017-08-18 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (303 votes). Check out past polls.

Notices?