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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^4: use constant and Compiler Optimizations by BrowserUk
in thread use constant and Compiler Optimizations by jbisbee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.