Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re^6: Hash versus chain of elsifsby eyepopslikeamosquito (Archbishop) |
on Nov 23, 2021 at 11:34 UTC ( [id://11139051]=note: print w/replies, xml ) | Need Help?? |
If performance is an issue, you might consider eliminating the lookup function call overhead (and the arguments about whether your module should use state or our or lexically scoped my ;-) simply by not writing a function at all! Instead performing your hash lookups directly. To test this idea, I wrote the following little benchmark:
Running the little benchmark program above on my laptop displayed:
Note that in the sample code above, to make a direct hash lookup more pleasing to the eye, I eliminated the call to exists simply by ensuring all keys have the true value 1. No surprise that using the hash directly is a lot faster than calling a function every time you do a lookup. Also of interest is that a hash lookup is only marginally faster than a hash_ref lookup. Based on this benchmark, rather than agonizing over whether your function should use block lexical scope or the Perl 5.10 state feature or an our variable, you might instead choose not to use a function at all! That is, perform the lookup directly via a hash, rather than a function call. Note that using a hashref, rather than a hash, gives you the flexibility to call your code with many different hashes, at a miniscule performance cost.
In Section
Seekers of Perl Wisdom
|
|