Benchmarking eval of strings, since you want to include compile time:

#!/usr/bin/perl -w use strict; my $foo = <<'BAR'; my $test = 1; #ifdef UNDEF $test = 0; #endif print "\$test => $test\n"; BAR my $bar = <<'BAZ'; use constant UNDEF => 0; my $test = 1; if (UNDEF) { $test = 0; } print "\$test => $test\n"; BAZ use Benchmark; =pod this is still wrong timethese (100000, { cpp => sub { eval "perl -P -e '$foo'";}, prl => sub { eval "perl -e '$bar'";} } ); =cut timethese (1000, { cpp => sub { system "perl -P -e '$foo'";}, prl => sub { system "perl -e '$bar'";} } );
Results are:
=pod $ perl bmp.pl Benchmark: timing 1000 iterations of cpp, prl... cpp: 88 wallclock secs ( 0.08 usr 0.54 sys + 52.56 cusr 29.98 +csys = 83.16 CPU) @ 1612.90/s (n=1000) prl: 49 wallclock secs ( 0.09 usr 0.56 sys + 33.78 cusr 12.19 +csys = 46.62 CPU) @ 1538.46/s (n=1000) =cut
Instead of forking a new interpreter each time, we can change the tests to:
timethese (10000, { cpp => sub { eval "$foo"; }, prl => sub { eval "$bar"; }}); =pod $ perl -P bmp.pl Benchmark: timing 10000 iterations of cpp, prl... cpp: 1 wallclock secs ( 0.50 usr + 0.02 sys = 0.52 CPU) @ 19 +230.77/s (n=10000) prl: 2 wallclock secs ( 2.40 usr + 0.03 sys = 2.43 CPU) @ 41 +15.23/s (n=10000) =cut
Pretty much a wash, at least for your code.

Update: Corrected code, was only firing up an interpreter before. U2: Still had it wrong, showing two ways of doing it with two very different results. It looks like the interpreter with preprocessor is slower to start, but compilation is indeed faster by a pretty wide margin. Your mileage will vary. ++ariels for catching my blunders so kindly.

After Compline,
Zaxo


In reply to Re: Preprocessor Pranks by Zaxo
in thread Preprocessor Pranks by cmilfo

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.