in reply to Re: correspondence between two arrays
in thread correspondence between two arrays

I see little to be 'sorry' about in your new code at Re: correspondence between two arrays -- aside from your opens (see the first bullet below for a preferred form). Unless that's the basis upon which other (aka "wiser"?) monks have downvoted the new node, I think the - -s are ill-justified: while 'XP is just a game,' some newcomers do still conflate rep and XP & mis-read a node's rep as a measure of its merit.

In any case, your latest would benefit from:

Please note also that the comment at line 7 in your first code (while -- arguably -- "technically correct") uses "hash" in a manner that's potentially misleading. Although PHP (for example) uses "hash" to describe the constructs there, the Perl venacular uses "array."

Replies are listed 'Best First'.
Re^3: correspondence between two arrays
by aaron_baugher (Curate) on Nov 24, 2011 at 20:23 UTC

    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.

    $ perl 939870.pl 1000 Building arrays of 1000 with unique 4-char keys... done. Arrays have 1000 items each. Benchmarking... Rate arrays plus regex using a hash arrays plus regex 2.70/s -- -100% using a hash 1000000000000000/s 37000000000000000% -- + $ perl 939870.pl 5000 Building arrays of 5000 with unique 4-char keys... done. Arrays have 5000 items each. Benchmarking... s/iter arrays plus regex using a hash arrays plus regex 9.40 -- -100% using a hash 1.00e-02 93900% -- $ perl 939870.pl 10000 Building arrays of 10000 with unique 4-char keys... done. Arrays have 10000 items each. Benchmarking... s/iter arrays plus regex using a hash arrays plus regex 46.3 -- -100% using a hash 3.00e-02 154267% -- $ perl 939870.pl 100000 Building arrays of 100000 with unique 4-char keys... done. Arrays have 100000 items each. Benchmarking... s/iter arrays plus regex using a hash arrays plus regex 4110 -- -100% using a hash 0.330 1245345% --

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

Re^3: correspondence between two arrays
by Lotus1 (Vicar) on Nov 24, 2011 at 22:22 UTC

    Your node is in reply to ansh batra yet you seem to be replying to anasuya. Are you confusing two similar sounding user ids or are you implying they are the same person? anasuya is the OP with only a few posts whereas ansh batra has over 60 posts in four months. Maybe people are downvoting an eager beaver who is muddying the water with not great advice after good advice has already been given.

    I also don't like to downvote newcomers if they are making a good effort and actually listen to helpful suggestions.