in reply to Re: What is the most efficient way to see if a hash is empty?
in thread What is the most efficient way to see if a hash is empty?
I'll let you draw your own conclusions
I would conclude that your benchmark may be flawed since for empty hashes the $j++ operation is skipped. Maybe that's the reason for the difference?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: What is the most efficient way to see if a hash is empty?
by BrowserUk (Patriarch) on Apr 28, 2009 at 09:41 UTC | |
I put the and ++$i there to stop the scalar keys %hash being optimised away for being in a void context:
A problem I've been bitten by with other useless statements in void contexts. And because it is far cheaper than an assignment:
and therefore less of a bias to the test. Do you have a better way to deal with these issues? Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
by shmem (Chancellor) on Apr 28, 2009 at 15:42 UTC | |
The least bias would be
Testing with empty %b gives
from which I would conclude that there's a minimal difference between testing an empty and a full hash. update: moritz code using these instead of sub {die}:
But! If I introduce a third test condition which also tests for %full, I get:
using a sub instead of a quoted string
Hmm... let's use moritz's code with strings instead of hashes:
It looks like the iteration/sub call overhead is shadowing the scalar %hash lookup, and moritz has been testing something else... I'm re-reading No More Meaningless Benchmarks! ;-) | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Apr 28, 2009 at 19:59 UTC | |
The trouble is, the comma operator does not prevent the "Useless use of ... in a void context:
And I'm never sure whether that means it will always be optimised away as with:
Or not? And with Benchmark not enabling warnings when it compiles stringified test code, it is very easy to find yourself benchmarking empty statements and drawing conclusions on that basis. And as you've demonstrated with your version of moritz benchmark, when attempting to benchmark micro-optimisations like the OPs question, the overhead of calling two levels of sub (benchmark wraps the user supplied code in it's own wrapper internally), can have a significant effect upon the perceived results. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |