Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^3: Comparing two arrays

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

in reply to Re^2: Comparing two arrays
in thread 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 }, } );
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067234]
[Corion]: Hmmm. Maybe I can still fake the one spot where I want/need a window function in SQLite... I think I'm never really interested in the real "next" image (not hidden/deleted)

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2018-03-23 17:53 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (295 votes). Check out past polls.