Dear Monks,
A friend of mine and I were having a conversation about the -P command line switch. We both have a C background and were talking about preprocessor commands in Perl. The -P switch gives Perl the ability to do Preprocessor #ifdef, #if, etc. (it should be noted that anyone using -P for the first time may want to double check all your comments, weird things can happen). Here is an example (contents of my test.pl):
my $test = 1;
#ifdef UNDEF
$test = 0;
#endif
print "\$test => $test\n";
Since I didn't put a #define UNDEF at the top of the code, the print statement prints $test => 1 when run with perl -P test.pl. Now, most of you are probably saying, "yeah, so what." Here's the debate. -P does the above preprocessing before compilation. This allows code to be added or ignored based on #define at the top of the program. In efforts to make the code safer and less system dependent, I suggested using
use constant UNDEF => 0;. Here is the same code with the use constant pragma:
use constant UNDEF => 0;
my $test = 1;
if (UNDEF) {
$test = 0;
}
print "\$test => $test\n";
Since the use constant pragma allows definition of a unchangable variable, if(), unless(), etc should be optimized out at the Top-Down Optimizer (constant folding) if there is no reference to a dynamic variable, function, etc. (i.e.
&dothis if UNDEF == $maychange;).
We reasoned that the preprocessor may be faster because the code would never get to the compilation phase. We aren't so sure, however, because firing up the preprocessor could be more costly than a clear foldable constant such as
&dothis if UNDEF;.
My questions for you, oh wise Monks, are as follows:
1) Which is faster, preprocessor (-P) or use constant pragma?
2) How did you find this out (better yet, aside from reading the internals of the perl interpreter -- I am not ready, is there a way to profile the compilation stage and where can I go to read/learn about such things)?
3) Once the optimizer strips out unnecessary code, at anytime in recompilation (such as when an eval() gets compiled at run-time), does this folded code effect the compilation time?
(And you thought I was going to start with, "What's your quest?") :)
As always, thank you. I've enjoyed the PerlMonks since I stumbled upon it a little over a year ago. The community amazes me over and over.
Cheers!
Casey
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.