I ran some benchmarks.
The two-liner outperformed the one-liners by a 10 to 1 ratio. Code and
results below.
Benchmark: timing 100000 iterations of OneLine, OneLine2, TwoLines...
OneLine: 41 wallclock secs (39.30 usr + 0.00 sys = 39.30 CPU) @ 2544.79/s
OneLine2: 34 wallclock secs (32.58 usr + 0.00 sys = 32.58 CPU) @
3069.56/s
TwoLines: 3 wallclock secs ( 2.58 usr + 0.00 sys = 2.58 CPU) @
38789.76/s
use strict;
use Benchmark;
my $val = " " . ("foo" x 200) . " ";
timethese(100_000, {
'OneLine' => sub{trimOne($val)},
'OneLine2' => sub{trimOne2($val)},
'TwoLines' => sub{trimTwo($val)},
});
sub trimOne {
my $s = shift;
$s =~ s/^\s+|\s+$//g;
die $s unless ($s eq ("foo"x200));
}
sub trimOne2 {
my $s = shift;
$s =~ s/^\s*(.*?)\s*$/$1/g;
die unless ($s eq "foo"x200);
}
sub trimTwo {
my $s = shift;
$s =~ s/^\s+//;
$s =~ s/\s+$//;
die unless ($s eq "foo"x200);
}
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Monday, September 29, 2003 8:04 PM
To: [EMAIL PROTECTED]
Subject: remove blanks
Is there a func or a onliner for removing blanks from both ends?
I'm using these:
$username =~ s/^\s+//;
$username =~ s/\s+$//;
There got to be one out there!
thanks,
-rkl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]