You called it right earlier. Two layers of subroutine call (your's and the one Benchmark adds) wrapped around such trivial amounts of code serves to swamp the code you're trying to test.
Get rid of one layer of subroutine call and add an internal multiplier and your results will be both more consistant and a truer reflection of the actual cost of what you are testing. At least they are on my system:
use strict;
use warnings;
use Benchmark qw( cmpthese );
use constant DEBUG => 0;
our $N ||= -3;
our $debug = 0;
our $b = 100;
cmpthese $N, {
constant => q{
for( 1 .. 1000 ){ my $a = 10 * $b; $a = 10 * $b if DEBUG; }
},
noconstant => q{ for( 1 .. 1000 ){ my $a = 10 * $b; } },
var => q{
our $debug;
for( 1.. 1000 ){ my $a = 10 * $b; $a = 10 * $b if $debug; }
},
};
__END__
C:\test>junk6 -N=-1
Rate var constant noconstant
var 2222/s -- -19% -19%
constant 2731/s 23% -- -1%
noconstant 2745/s 24% 1% --
C:\test>junk6 -N=-3
Rate var constant noconstant
var 2133/s -- -20% -25%
constant 2662/s 25% -- -6%
noconstant 2831/s 33% 6% --
C:\test>junk6 -N=-10
Rate var constant noconstant
var 2172/s -- -18% -21%
constant 2659/s 22% -- -3%
noconstant 2746/s 26% 3% --
C:\test>junk6 -N=-20
Rate var constant noconstant
var 2171/s -- -18% -21%
constant 2632/s 21% -- -5%
noconstant 2759/s 27% 5% --
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.
|