----- Original Message -----
From: David H. Adler <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, May 02, 2001 8:46 PM
Subject: Re: String deconstruction?
> On Wed, May 02, 2001 at 11:28:14AM -0700, Ross Larner wrote:
> > Hello. I am attempting to print a string one character at a time.
Right now I am using one while loop with chop to create a new string, the
reverse of the original string, then another while loop with chop on the
reversed string to print out the characters. I'm sure there's a more
straight-forward way of doing this - any ideas?
>
> 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????
No need to iterate i guess...
use Benchmark;
$x = "huzzah!";
$times = shift || 10000;
my @chars;
timethese($times, {
splitter => sub {@chars=split //, $x},
substring => sub {for (0..length($x) - 1){push @chars, $_ }}
});
Benchmark: timing 50000 iterations of splitter, substring...
splitter: 1 wallclock secs ( 0.50 usr + 0.00 sys = 0.50 CPU) @
100000.00/s
(n=50000)
substring: 1 wallclock secs ( 0.49 usr + 0.00 sys = 0.49 CPU) @
102040.82/s
(n=50000)