Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

conditional debug call perf

by greatdane (Beadle)
on Jan 25, 2007 at 00:34 UTC ( #596376=perlquestion: print w/replies, xml ) Need Help??

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

I have a ton of debug calls in my code like the following.
&foo_debug ("<message>") if ($FOO_DEBUG_MODE);
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!

Replies are listed 'Best First'.
Re: conditional debug call perf
by ysth (Canon) on Jan 25, 2007 at 01:48 UTC
    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
      Thank you! This is exactly what I was looking for!
Re: conditional debug call perf
by SheridanCat (Pilgrim) on Jan 25, 2007 at 00:45 UTC
    You might want to take a look at Log::Log4Perl. I can't say it's going to reduce your performance overhead, but it will make your logging (for debugging and other purposes) more flexible.

    Have you profiled or benchmarked your code to see if those "if" statements are really causing any performance issues? Usually you'll find those bottlenecks elsewhere, such as in file I/O or database calls.

    Here's an article on Log4Perl you might find useful: Retire your debugger....

      Thanks for the pointer. I will look into using that within my debug function.
Re: conditional debug call perf
by ikegami (Patriarch) on Jan 25, 2007 at 01:02 UTC

    Have you measured the overhead the debugging calls add? In other words, are you sure that moving the if into foo_debug will make a noticeable difference?

      I have not measured. Yet, I have tons of these debug calls with many inside critical hefty loops so I assume that every fraction of a second will help.

        Don't assume. Measure.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2023-01-27 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?