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. :-)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)
In reply to Re: Count of words that match in an array
by BUU
in thread Count of words that match in an array
by Al Shiferaw
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |