
mhx
Enthusiast
/ Moderator
Nov 26, 2002, 4:33 AM
Post #9 of 9
(673 views)
|
|
Re: [RedRum] White Spaces in the middle
[In reply to]
|
Can't Post
|
|
Still, the first two could be made into one :) I guess you mean like so: However, the performance of this can be lousy, as the following benchmark shows:
Benchmark: timing 100000 iterations of mhx, one, two... mhx: 2 wallclock secs ( 1.95 usr + 0.00 sys = 1.95 CPU) @ 51282.05/s (n=100000) one: 12 wallclock secs (11.77 usr + 0.01 sys = 11.78 CPU) @ 8488.96/s (n=100000) two: 5 wallclock secs ( 5.08 usr + 0.01 sys = 5.09 CPU) @ 19646.37/s (n=100000) Where one is the solution with one regex, and two is for the original solution with two separate regexes. Oh, and mhx is my optimized solution.
use Benchmark; my $str = ' test test test test test test '; timethese( 100000, { one => sub { my $s = $str; $s =~ s/^\s+|\s+$//g; $s =~ s/\s+/ /g; }, two => sub { my $s = $str; $s =~ s/^\s+//; $s =~ s/\s+$//; $s =~ s/\s+/ /g; }, mhx => sub { my $s = $str; $s =~ y/ //s; $s =~ s/^\s//; $s =~ s/\s$//; }, } ); -- mhx
At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."-- Frodo
|