This "constant" pragma is a cool thing. If DEBUG => 0;, Perl will realize that do_debug() if DEBUG; is never going to do anything at all, could never be executed and therefore won't even compile anything into the code. So you can leave a bunch of debug stuff in the production code without incurring any run time penalty at all in the non-debug case. I typically use ALLCAPS for constants like this. It is easier for me to spot them in the code.use constant DEBUG => 1; do_debug() if DEBUG;
So, in the context of your post, there can't be anything faster than doing nothing at all in the non-debug case. Also often in my debug code, usually some sort of I/O operation is going to happen which is so slow that whether it is in a subroutine or not makes absolutely no difference in the scheme of things.
update: Usually instead of just "do_debug()", I put some name on that subroutine like perhaps "dump_decision_tree() if DEBUG;". I do that even if this debug code could only be called once. The name of the sub tells me what it would do without having to look further. I don't have to skip past "inline" debug code. I usually do that for anything more than a simple "print".
In reply to Re: Embedded function or subroutine - which is better?
by Marshall
in thread Embedded function or subroutine - which is better?
by Linicks
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |