http://qs321.pair.com?node_id=740981


in reply to mathematical proof

If you're trying to prove this for every file, I think you're doomed. In the special case where the file's keys are all small integers, the array will work faster than the hash. Operations on the array are generally faster, and its storage will be more compact to boot.

It's a rare special case, to be sure. Still, my point is that the input to some algorithm can have a significant effect on how efficient it is.

Consider this case I was testing one day:

for ( grep { filter($_) } @big_array ) { # do stuff }

vs.

for ( @big_array ) { next if ! filter($_); # do stuff }

Which is faster? It depends on how much of @big_array gets eliminated by the filter(). If most of the array is filtered out, it's faster to do it with grep. If little of the array is filtered out, it's faster to skip the extra loop (grep).

All that having been said, I'd use a hash for the task that you describe. I just wouldn't try to prove that it will always be the fastest way to do it.