in reply to Re^2: Benchmark of hash emptiness test
in thread Benchmark of hash emptiness test
The reason for the speed difference is because if (%hash) {} ends being implemented internally as
sv = sv_newmortal(); if (HvFILL((HV*)hv)) Perl_sv_setpvf(aTHX_ sv, "%ld/%ld", (long)HvFILL(hv), (long)HvMAX(hv) + 1); else sv_setiv(sv, 0);
So what happens is a new SV is created, its then populated with a string using something like sprintf.
This can be compared against the keys %hash option where an extra opcode is executed, BUT that opcode involves creating an SvIV only and therefore requires no memory allocation, no conversion of longs to strings, etc.
So the bottom line is that if you are concerned about speed use the keys form. In Perl 5.10 we will try to make this an internal optimisation, (internally using if (keys %foo) when the user typed if (%foo) )but its not exactly priority, at least not for me :-).
Update: Well, I gave it a try just to see what was involved, and before I knew it I was sending off patches. So theres a half decent chance this will be fixed in perl 5.10
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Benchmark of hash emptiness test
by Hofmator (Curate) on Nov 10, 2006 at 10:55 UTC | |
by demerphq (Chancellor) on Nov 10, 2006 at 13:02 UTC | |
|
Re^4: Benchmark of hash emptiness test
by aufflick (Deacon) on Nov 12, 2006 at 12:12 UTC |