Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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. :-)

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2022-08-16 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?