summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo van der Sanden <hv@crypt.compulink.co.uk>1997-04-01 23:31:55 +0100
committerChip Salzenberg <chip@atlantic.net>1997-04-01 12:01:35 +1200
commit446534a1e286d6fde0fd79424865d3cf1291546b (patch)
treee2d9973dae347624625080db5c5d189b38aea4e0
parent7e5dee47ee5bc43e90e14d8c27c33a42dd0a26ae (diff)
downloadperl-446534a1e286d6fde0fd79424865d3cf1291546b.tar.gz
Document eval vs. sub in Benchmark
Subject: Re: Patch for Benchmark.pm In <9704011547.AA29906@toad.ig.co.uk>, Tim Bunce writes: :Simple: calling functions in perl is expensive. Yeah, I simply hadn't expected it to be *that* expensive. :> - ? "sub { package $pack; my(\$_i)=$n; while (\$_i--){&\$c;} }" :> + ? "sub { package $pack; my(\$_i)=$n; while (\$_i--){&\$c();} }" :It's cheaper to _not_ add () so I'd recommend leaving it off. I considered that, but felt it was more correct not to pass through the parameters that were received. My new patch ducks it, by shifting @_ out before it gets there. I also looked at having two caches for empty string/empty sub timings, but was unable to get satisfactory results. I'll come back to that one, but I don't think it is urgent for 5.004. This patch is for the Benchmark.pm in _96: it repeats stuff from my previous patch, but avoids stuff that was obsoleted by other changes in _96. p5p-msgid: 199704012231.XAA00225@crypt.compulink.co.uk
-rw-r--r--lib/Benchmark.pm8
1 files changed, 6 insertions, 2 deletions
diff --git a/lib/Benchmark.pm b/lib/Benchmark.pm
index a3c8544002..67bdcd2a9f 100644
--- a/lib/Benchmark.pm
+++ b/lib/Benchmark.pm
@@ -176,6 +176,10 @@ for Exporter.
=head1 CAVEATS
+Comparing eval'd strings with code references will give you
+inaccurate results: a code reference will show a slower
+execution time than the equivalent eval'd string.
+
The real time timing is done using time(2) and
the granularity is therefore only one second.
@@ -258,7 +262,7 @@ sub timestr {
my($pt, $ct, $t) = ($tr->cpu_p, $tr->cpu_c, $tr->cpu_a);
$f = $defaultfmt unless defined $f;
# format a time in the required style, other formats may be added here
- $style = $defaultstyle unless defined $style;
+ $style ||= $defaultstyle;
$style = ($ct>0) ? 'all' : 'noc' if $style eq 'auto';
my $s = "@t $style"; # default for unknown style
$s=sprintf("%2d secs (%$f usr %$f sys + %$f cusr %$f csys = %$f cpu)",
@@ -278,7 +282,7 @@ sub timedebug {
# --- Functions implementing low-level support for timing loops
sub runloop {
- my($n, $c) = @_;
+ my($n, $c) = (shift, shift);
$n+=0; # force numeric now, so garbage won't creep into the eval
croak "negative loopcount $n" if $n<0;