Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Count of words that match in an array

by BUU (Prior)
on May 10, 2002 at 22:03 UTC ( [id://165786]=note: print w/replies, xml ) Need Help??


in reply to Count of words that match in an array

use Benchmark; @Array1 = ("A", "B", "C", "D"); @Array2 = ("J", "C", "T", "A"); %seen = (); timethese(1_000_000, { 'stephen'=> sub { %seen=();foreach (@Array1) { $seen{$_}++ }; my $count = 0; foreach (@Array2) { exists $seen{$_} and $count++; } }, 'Russ'=> sub { @Hash1{@Array1} = undef; $matches=scalar grep {exists $Hash1{$_}} @Array2; }, 'BUU'=> sub { $matches=grep{$x=$_;grep{$_ eq $x} @Array1} @Array2; }, 'Perlfaq'=> sub { @union = @intersection = @difference = (); %count = (); foreach $element (@Array1, @Array2) {$count{$element}++ foreach $element (keys %count) { push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@difference }, +$element; } } }); __DATA__ Benchmark: timing 1000000 iterations of BUU, Perlfaq, Russ, stephen... BUU: 14 wallclock secs (15.17 usr + 0.00 sys = 5.17 CPU) @ 659 +19.58/s (n=1000000) Perlfaq: 58 wallclock secs (57.13 usr + 0.00 sys = 7.13 CPU) @ 175 +03.94/s (n=1000000) Russ: 6 wallclock secs ( 5.12 usr + 0.00 sys = 5.12 CPU) @ 195 +312.50/s (n=1000000) stephen: 17 wallclock secs (15.99 usr + 0.01 sys = 6.00 CPU) @ 625 +00.00/s (n=1000000)
Editor's Note: Corrected minor code error and re-ran results. Also, note that Perlfaq computes more than the simple intersection, so its results may be misleading at first glance. Using a hash is still the clear winner, Russ' comment notwithstanding. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://165786]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-23 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found