I was just wondering about the efficiency of using a one dimensional hash (i.e. only keys, no values – we don’t care about them anyway) over a one dimensional array.
The main reason I wanted to use a hash for this purpose is so I can use the exists function to see if an ‘entry’ already exists. Hashes are also great for not duplicating keys right? With arrays, I would need to set up my own checks involving grep which I’m led to believe would be slower.
This hash/array will then be iterated through for certain operations.
I’d love to hear any insight on this, and thanks in advance!
is a nice, short expression, clear and easy to use. Obviously
Is not quite as short, but
is even shorter. So since 5.10, it just as syntactically easy to test the smart match as the
exists.So guessing from the performance difference between arrays and hashes, I can imagine that the smart match will perform faster for a small list of items, but the hash will by far outperform the array lookup with a large list of items.
However, you should Benchmark the performance anyway.
And this is why. On Strawberry perl, with even a list size 1, the hash look-up outperforms the string match:
With 2 items in the list:
And with 20 items:
I would use the hash.