good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re^6: please help me to resolve the Line comments and appending issueby aaron_baugher (Curate) |
on Aug 10, 2012 at 15:46 UTC ( [id://986766]=note: print w/replies, xml ) | Need Help?? |
Almost always true in practice. But there is more overhead in a hash than in an array, and that's magnified if you already have an array and have to duplicate it as a hash. If you're going to check more than one value against the list, as in the FAQ "How do I search file2 for all the values in file1?" then a hash is almost certainly the best solution. But if you only have to check one or two values, you may be better off sticking with grep:
So if I only need to search the list once, grep wins over a hash. For multiple searches, a hash comes out ahead. List::Util's first() routine splits the difference a little; with my dataset it beats the hash for up to about 8 searches, but I assume that would vary greatly depending on how early in the array the match is made. It'd take more testing with matches found earlier/later/never in the array to form a good comparison there. My simple conclusion would be: always build a hash unless you know your program will only search it once; then use grep or List::Util::first. Also, if I'm pulling data in from somewhere (like a file) and I know I'm going to be searching it this way, I put it straight into a hash from the start. Aaron B.
In Section
Seekers of Perl Wisdom
|
|