in reply to Re^10: Finding the size of a nested hash in a HoH
in thread Finding the size of a nested hash in a HoH
I noticed from your code, I was very unfair to while/each because I lack value assignment with foreach/keys test function. When I change Benchmark test function fromforeach my $k (keys %h){ }
Look at the code again carefully. When the command line argument -M2=pairs the code run for the for / keys test is:
else { if( $M2 eq 'pairs' ) { for my $k ( keys %hash ) { my $v = $hash{ $k }; ++$count; } }
Ie. This is equivalent to the while / each case for -M2=pairs of:
if( $M1 eq 'each' ) { if( $M2 eq 'pairs' ) { while( my( $k, $v ) = each %hash ) { ++$count; } }
To perform the test for both cases where only the keys are accessed, use a command line argument of -M2=keysonly (or any value other than pairs). The code that gets run for two methods in this case are:
else { while( my $k = each %hash ) { ++$count; } }
and
else { for my $k ( keys %hash ) { ++$count; } }
Again, equivalent code.
Thanks for confirming my premise.
|
---|