Perl-Sensitive Sunglasses | |
PerlMonks |
Re^3: correspondence between two arraysby aaron_baugher (Curate) |
on Nov 24, 2011 at 20:23 UTC ( [id://939949]=note: print w/replies, xml ) | Need Help?? |
I'd guess the downvotes (though I agree that may be a harsh response to a solution that could be much better but does work) are probably because he's looping through the entire second array for every item in the first array, instead of using a hash. Writing the above got me curious: Just how bad is looping through an array and comparing as opposed to checking against a hash's keys? Very, very bad, it turns out. I wrote the script below, based on this task, to benchmark the two methods with a variable number of items in the lists. I only used one iteration for Benchmark (and removed the warnings to save space), because the test takes long enough to do once with larger lists, and I didn't want it to take all day. But the differences are large enough to ignore whatever margin of error that more iterations would smooth out. The hash method also has the overhead of splitting the elements of the second array and creating the hash. With only 1000 items in each list, the array/array method takes about 2.75 seconds -- fast enough to live with, if you're not running it over and over all day. But the hash method is so fast that Benchmark seems unable to display the time elapsed sensibly. Bumping the lists up to 5000 items each, the array/array method goes up to almost 10 seconds, but the hash method is still down at .01 seconds, or almost 1000 times faster. At 10,000 items, array/array is up to 45 secs, and the hash is still at .02, or 1500 times faster. So not only is the array/array method much slower, but it scales much worse, since the number of loops and comparisons increases by the square of the list size. At 100,000 items in each array, the array/array method took well over an hour, and the hash method is still under a second! I knew the hash would be faster, but wow.
Aaron B.
In Section
Seekers of Perl Wisdom
|
|