On Wed, May 02, 2001 at 09:00:27PM +0200, M.W. Koskamp wrote:
>
> ----- Original Message -----
> From: David H. Adler <[EMAIL PROTECTED]>
> > For what it's worth two ideas present themselves to me.
> >
> > ------code--------
> >
> > use Benchmark;
> >
> > $x = "huzzah!";
> > $times = shift || 10000;
> > timethese($times, {
> > splitter => sub {for (split //, $x){ $q = $_ }},
> > substring => sub {for (0..length($x) - 1){$q = $_ }}
> > });
>
> Why iterating over the split list????
Because what he actually wants to do is print them, but I didn't wan't
to look at all those letters being printed out 1 by 1, but I wanted both
subs to be doing the same thing. A simple assignment jumped up at me
and I was in a hurry. More to the point, I was *trying* to show the two
approaches, but I screwed up the code. Fixed (unless I'm even more out
of it than I thought), including actually making it print, but not so I
have to look at it...:
----code----
use Benchmark;
open JUNK, ">/dev/null";
$x = "huzzah!";
$times = shift || 10000;
timethese($times, {
splitter => sub {for (split //, $x){ print JUNK $_; }},
substring => sub {for (0..length($x) - 1){print JUNK substr($x,
$_, 1); }}
});
----end code---
results from that are:
Benchmark: timing 500000 iterations of splitter, substring...
splitter: 12 wallclock secs (10.34 usr + 0.11 sys = 10.45 CPU)
substring: 7 wallclock secs ( 7.62 usr + 0.01 sys = 7.63 CPU)
Dang, substr() is fast. :-)
dha, wondering how long a string that would still be faster for...
--
David H. Adler - <[EMAIL PROTECTED]> - http://www.panix.com/~dha/
America leads the world in shocks.
- Gil Scott-Heron