Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: conditional debug call perf

by ysth (Canon)
on Jan 25, 2007 at 01:48 UTC ( [id://596385]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://596385]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-25 22:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found