ManFromNeptune has asked for the wisdom of the Perl Monks concerning the following question:

Question about performance and perl code compilation:

I'm developing a web app (mod_perl), with copious logging conditionals sprinkled throughout the code. At the top of each module, I've got a set of constants defined with logging levels, and then a final constant "LOG_LEVEL":

use constant LOG_DEBUG => 70; use constant LOG_INFO => 60; use constant LOG_WARN => 50; use constant LOG_NONE => 0; . . # LOG_LEVEL is set to the desired LOG_* constant, to # determine the amount of logging detail use constant LOG_LEVEL => LOG_DEBUG; . . . # logging statements like this, throughout the code.... if ( LOG_LEVEL >= LOG_DEBUG ) { # do some logging here }

My question is, does the perl compiler throw out code (conditional statements) that have no chance of ever evaluating to true? i.e., in the example above, if I had set "use constant LOG_LEVEL => LOG_WARN", then that logging conditional would never run, so would perl actually evaluate it during runtime?

I'd like to think that when I'm ready for production, for performance I can just set my LOG_LEVEL to LOG_NONE, and none of those conditionals will ever be evaluated.

Lastly, I'm aware that using scalers (i.e., $LOG_LEVEL = $LOG_WARN) are perhaps slightly faster than constants, but if my assumption about perl throwing out this code is correct, then it shouldn't matter, right?

Thanks in advance for the help... something tells me this is a SFAQ (somewhat-frequent...) :)

cheers
MFN

Replies are listed 'Best First'.
Re: using constants to trigger debug code
by Belgarion (Chaplain) on Jun 04, 2004 at 19:43 UTC

    If the compiler can determine at compile time that an expression will never be evaluated, it will remove the code. For example:

    use strict; use warnings; print "Hello world" if 0; exit 1 if 0; exit 0;

    looks like this to the Perl compiler:

    $ perl -MO=Deparse /tmp/compile.pl use warnings; use strict 'refs'; '???'; '???'; exit 0;

    The two .. if 0; lines are turned into void strings which take basically no time to evaluate.

Re: using constants to trigger debug code
by BrowserUk (Patriarch) on Jun 04, 2004 at 19:51 UTC
    perl -MO=Deparse -e"use constant DEBUG=>0; if( DEBUG ) { print 'hello' +; }" use constant ('DEBUG', 0); '???'; -e syntax OK perl -MO=Deparse -e"use constant DEBUG=>1; if( DEBUG ) { print 'hello' +; }" use constant ('DEBUG', 1); do { print 'hello' }; -e syntax OK

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: using constants to trigger debug code
by liz (Monsignor) on Jun 04, 2004 at 20:03 UTC
    To further what the other monks already said.

    When using constants with debugging, I'm usually using an environment variable to signal the level of debugging info required, with variations on the following code:

    BEGIN { my $level = $ENV{'FOO_DEBUG'} || 0; eval "*Foo::DEBUG = sub () {$1}" if $level =~ m#^(\d+)$#s; } #BEGIN if (Foo::DEBUG) { print "We're debugging\n"; }
    If this code is run with -MO=Deparse, you get:
    sub BEGIN { my $level = $ENV{'FOO_DEBUG'} || 0; eval "*Foo::DEBUG = sub () {$1}" if $level =~ /^(\d+)$/s; } '???';
    If you set the environment variable FOO_DEBUG to 1, you get:
    sub BEGIN { my $level = $ENV{'FOO_DEBUG'} || 0; eval "*Foo::DEBUG = sub () {$1}" if $level =~ /^(\d+)$/s; } do { print "We're debugging\n" };

    Hope this helps.

    Liz