in reply to Re^2: use constant and Compiler Optimizations
in thread use constant and Compiler Optimizations

Yes, but funnily enough, doing an actual boolean test on an integer appears to sometimes be faster on my machine than doing the optimization. I'm not sure why, and the results vary. The output of my test script above with -10 and -2 as the first argument to timethis() cmpthese() respectively is:

Rate var constant noconstant var 5014619/s -- -10% -40% constant 5551194/s 11% -- -33% noconstant 8292829/s 65% 49% --
Rate constant var noconstant constant 5505020/s -- -17% -23% var 6604512/s 20% -- -7% noconstant 7120433/s 29% 8% --

Replies are listed 'Best First'.
Re^4: use constant and Compiler Optimizations
by BrowserUk (Patriarch) on Jul 18, 2008 at 22:26 UTC

    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.