Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is a presupposition here, that the most efficient way to do a lot of "Does the array contain X?" checks is to convert the array to a hash. Efficiency depends on the scarce resource, often time but sometimes space and sometimes programmer effort. You are at least doubling the memory consumed by keeping a list and a hash of the the list around simultaneously. Plus you're writing more original code that you'll need to test, document, etc.</p> <p>As an alternative, look at the List::MoreUtils module, specifically the functions <code>any()</code>, <code>none()</code>, <code>true()</code> and <code>false()</code>. They all take a block as the conditional and a list as the argument, similar to <code>map()</code> and <code>grep()</code>:</p> <p><code>print "At least one value undefined" if any { !defined($_) } @list;</code></p> <p>I ran a quick test, loading in half of /usr/share/dict/words to an array (25000 words), then looking for eleven words selected from across the whole dictionary (every 5000th word) in the array, using both the array-to-hash method and the <code>any()</code> function from List::MoreUtils.</p> <p>On Perl 5.8.8 built from source, the array-to-hash method runs almost 1100x faster than the <code>any()</code> method (1300x faster under Ubuntu 6.06's packaged Perl 5.8.7.)</p> <p>That's not the full story however - the array-to-hash conversion takes about 0.04 seconds which in this case kills the time efficiency of array-to-hash method to 1.5x-2x faster than the <code>any()</code> method. Still good, but not nearly as stellar.</p> <p>My gut feeling is that the array-to-hash method is going to beat <code>any()</code> in most cases, but I'd feel a whole lot better if I had some more solid metrics (lots of test cases, decent statistical analyses, maybe some big-O algorithmic analysis of each method, etc.) Depending on your needs, List::MoreUtils may be a better solution; it's certainly more flexible and requires less coding. Remember, premature optimization is a sin... :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload