Well here it is: Proof that you can really perform a task in multiple ways. I needed an efficient way to cut off trailing whitespace. I was pulling values from a database and the scalars all had trailing whitespace. I had mistakenly made the column type CHAR(10) when it should have been a VARCHAR. Let me just say that the details of the job make it alright to cut the whitespace instead of fixing the underlying problem
So I had three ideas (none original) to perform this task. Here is my benchmark script.
use Benchmark; timethese (1000000, { 'unpack' => q{ my $foo = "test "; $foo = unpack("A8",$foo); }, 'regex' => q{ my $foo = "test "; $foo =~ s/\s+$//;}, 'xeger' => q{ my $foo = reverse "test "; $foo =~ s/^\s+//; $foo = reverse $foo;} } );
The standard regex, the unpack function, and the reverse regex. I figured the reverse one maybe wouldn't apply because the is no .* in the match. But it is cool enough an idea to try out.
Here are my results:
Benchmark: timing 1000000 iterations of regex, unpack, xeger... regex: 6 wallclock secs ( 5.71 usr + 0.00 sys = 5.71 CPU) @ 17 +5131.35/s (n=1000000) unpack: 7 wallclock secs ( 5.91 usr + 0.00 sys = 5.91 CPU) @ 16 +9204.74/s (n=1000000) xeger: 9 wallclock secs ( 9.26 usr + 0.00 sys = 9.26 CPU) @ 10 +7991.36/s (n=1000000)
So it seems that the regex and the unpack are very close to the same time. Notice that this is for 1 million iterations. All methods are fast. So you can use various methods for this task!
Thank you, goodnight.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Removing Trailing Whitespace: Multiple ways.
by no_slogan (Deacon) on May 11, 2001 at 22:43 UTC | |
Re: Removing Trailing Whitespace: Multiple ways.
by perlmonkey (Hermit) on May 12, 2001 at 02:53 UTC | |
by no_slogan (Deacon) on May 12, 2001 at 03:15 UTC | |
by perlmonkey (Hermit) on May 12, 2001 at 04:18 UTC | |
Re: Removing Trailing Whitespace: Multiple ways.
by tune (Curate) on May 11, 2001 at 23:07 UTC |