go ahead... be a heretic | |
PerlMonks |
Re^4: Optimize a perl blockby kcott (Archbishop) |
on Sep 07, 2017 at 23:26 UTC ( [id://1198891]=note: print w/replies, xml ) | Need Help?? |
"... keys ... isn't guaranteed to return a constant value so your function couldn't be inlined." I disagree with that in part. I did take into consideration this from the keys doco: "Hash entries are returned in an apparently random order." Along with this, a couple of sentences later: "So long as a given hash is unmodified you may rely on keys, values and each to repeatedly return the same order as each other. " A very quick and dirty test (which I ran a number of times) with
always returned one of these two:
So, that's a reasonable confirmation of the doco, and the fact that my Perl version (5.26.0, by the way) is adhering to this. Anyway, I went back to the code I posted earlier, and added the subroutine requesting inlining back in; however, this time I wrote it like this, so that @keys was constant and couldn't possibly be altered elsewhere due to the lexical scoping of the anonymous block:
There was no improvement on the OP's code. I ran it a few times, the "kens/op:" value was now showing 100% (±2%). With regard to the caching, that's the reason I didn't spend any time trying to check it. The minimal sanity checking (that the OP's and my code were producing equivalent results) showed %$bar had one key ("1275") and its arrayref value held all of the two million elements. — Ken
In Section
Seekers of Perl Wisdom
|
|