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

In reply to Re: conditional debug call perf by ysth
in thread conditional debug call perf by greatdane

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.