in reply to conditional debug call perf

I am always qualifying the calls with an "if" to avoid the cost of a call/return pair. Is there anything else I can do to eliminate more of the performance overhead? A better method? BTW, I want the debug calls to be an execution time option. The program has a usage with a "-debug" option. Thanks!
Having a -debug option doesn't mean you need to do anything at execution time. You can check options in a BEGIN block and set a constant that controls whether debug statements will be optimized away or not:
$ cat deb.pl #!/usr/bin/perl use strict; use warnings; use Getopt::Long; my $FOO_DEBUG_MODE; BEGIN { GetOptions("debug", \$FOO_DEBUG_MODE) } use constant FOO_DEBUG_MODE => $FOO_DEBUG_MODE; print "debug statment\n" if FOO_DEBUG_MODE; print "done!\n";
Without the -debug option, the print and if are optimized away to be just an empty statement:
$ perl -MO=Deparse deb.pl use Getopt::Long; BEGIN {${^WARNING_BITS} = "UUUUUUUUUUUU"} use strict 'refs'; my $FOO_DEBUG_MODE; sub BEGIN { GetOptions 'debug', \$FOO_DEBUG_MODE; } use constant ('FOO_DEBUG_MODE', $FOO_DEBUG_MODE); '???'; print "done!\n"; deb.pl syntax OK
With -debug, the if is optimized away leaving just the debug call:
$ perl -MO=Deparse deb.pl -debug use Getopt::Long; BEGIN {${^WARNING_BITS} = "UUUUUUUUUUUU"} use strict 'refs'; my $FOO_DEBUG_MODE; sub BEGIN { GetOptions 'debug', \$FOO_DEBUG_MODE; } use constant ('FOO_DEBUG_MODE', $FOO_DEBUG_MODE); print "debug statment\n"; print "done!\n"; deb.pl syntax OK

Replies are listed 'Best First'.
Re^2: conditional debug call perf
by greatdane (Beadle) on Jan 26, 2007 at 06:39 UTC
    Thank you! This is exactly what I was looking for!