Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Optimize debugging subs

by danb (Friar)
on Apr 05, 2006 at 03:20 UTC ( [id://541263]=perlquestion: print w/replies, xml ) Need Help??

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

Is it possible to optimize debugging subs into no-ops at compile time?
I already know one method of doing so, but it's inelegant:
use constant ENABLE_DEBUG => 0; sub debug { print $_[0]; } debug 'test' if ENABLE_DEBUG;
You can see the call gets optimized away in the Deparse output:
use constant ('ENABLE_DEBUG', 0); sub debug { print $_[0]; } '???';
However, the more elegant equivalent...
use constant ENABLE_DEBUG => 0; sub debug { print $_[0] if ENABLE_DEBUG(); } debug 'test';
...does not get optimized away:
use constant ('ENABLE_DEBUG', 0); sub debug { 0; } debug 'test';
Update 1: diotalevi convinced me to use constant. Update 2: I stumbled on Smart::Comments, which is very interesting, but I don't know if it could be made to work with Log::Log4perl. Plus, it's a source filter (take that as you will).

--Dan

Replies are listed 'Best First'.
Re: Optimize debugging subs
by diotalevi (Canon) on Apr 05, 2006 at 04:21 UTC

    You'd want to prefer use constant to sub ... () { ... }. Perl core keeps constant more up to date with what's right for the version that's running your perl than your code will ever be. For instance, the 5.9.3+ version is best expressed in a manner *other* than a simple sub ... () { ...}. If you use constant, perl will do the right thing for the perl version you're using. If you're doing it by hand, you may be doing it suboptimally.

    Oh yeah, forget your idea of the "elegant" version. It's just going to be more overhead than the DEBUG && foo(...);/foo(...) if DEBUG; version because it can't avoid calling foo(...). Perl doesn't know that &foo won't be redefined later so it's left as a function call.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Optimize debugging subs
by ikegami (Patriarch) on Apr 05, 2006 at 05:13 UTC

    A (non-portable) option is to invoke CPP by using -P, as documented in perlrun.

    #!/usr/bin/perl -P use constant DEBUG => 0; sub _debug { print $_[0]; } #define debug(s) _debug(s) if DEBUG debug("test\n");

    The docs say "A -P on a #! line doesn't work.", but it worked on my system.

    The docs say "The #! line is stripped, so any switches there don't apply.", but they worked on my system.

Re: Optimize debugging subs
by roboticus (Chancellor) on Apr 05, 2006 at 03:29 UTC
    danb--

    Perhaps a compromise based on something like the following attempt? (Disclaimer: I've not tried this, nor have I ever used Deparse...)

    use constant DBG => 0; . . . DBG && print "Any debugging statement\n";
    If it works, it has the advantage that you can use a larger range of debug helper type statements after the && operator...

    --roboticus

      Thanks for the suggestion, but that is just as inelegant.

      --Dan

Re: Optimize debugging subs
by Anonymous Monk on Apr 05, 2006 at 03:34 UTC
    Your more elegant equivalent achives the same leval of optimization, print $_[0] if ENABLE_DEBUG; evaluates to false.
      This benchmark indicates that you are wrong.
      sub ENABLE_DEBUG () { 0 } sub debug_inelegant { print $_[0]; } sub debug_elegant { print $_[0] if ENABLE_DEBUG; } use Benchmark qw(:all) ; cmpthese( 30000000, { 'inelegant' => "debug_inelegant 'test' if ENABLE_DEBUG;", 'elegant' => "debug_elegant 'test';" }, );
      Output:
      Rate elegant inelegant elegant 3337041/s -- -95% inelegant 62500000/s 1773% --

      --Dan

        According to your benchmark, elegant takes 0.0000003 seconds (0.3 microseconds) to execute. What's the problem?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found