in reply to Preprocessor Pranks

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

Replies are listed 'Best First'.
Re: Re: Preprocessor Pranks
by ariels (Curate) on May 05, 2002 at 13:45 UTC
    Comparing the results of this Benchmark with my benchmark just below yields a startling discrepancy. My computers can barely manage 25 iterations a second, whereas Zaxo's seem to finish 15_000 iterations a second easily!

    Actually, there's still a problem with the code above: it's measuring how long it takes to ``eval "perl -P -e '$foo'"'' (and something similar for $bar). But that's just string concatenation, with a useless eval thrown in (it fails; that's not legal Perl code!). You need actually to run the command line, say by s/eval/system/g;.