RE: remove blanks
On Sep 29, Hanson, Rob said: >I ran some benchmarks. > >The two-liner outperformed the one-liners by a 10 to 1 ratio. Code and >results below. > >Benchmark: timing 10 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 You'll get a better representation with the following benchmark: #!/usr/bin/perl use Benchmark 'cmpthese'; my $s = " " . join(" ", ('foo') x 100) . " "; cmpthese(-5, { capture => \&capture, alt => \&alt, two => \&two, two_rev => \&two_rev, }); sub capture { my $copy = $s; $copy =~ s/^\s*(.*?)\s*$/$1/s; } sub alt { my $copy = $s; $copy =~ s/^\s+|\s+$//g; } sub two { my $copy = $s; s/^\s+//, s/\s+$// for $copy; } sub two_rev { my $copy = $s; $copy =~ s/^\s+//; ($copy = reverse $copy) =~ s/^\s+//; $copy = reverse $copy; } The output is thus: Benchmark: running alt, capture, two, two_rev for at least 5 CPU seconds alt: 5 wallclock secs @ 4163.86/s (n= 21777) capture: 5 wallclock secs @ 4876.60/s (n= 25846) two: 6 wallclock secs @ 24841.44/s (n=130666) two_rev: 5 wallclock secs @ 148958.27/s (n=774583) Rate alt capture two two_rev alt 4164/s ---15%-83%-97% capture 4877/s 17% ---80%-97% two 24841/s497%409% ---83% Notice the string this time. It's got embedded whitespace. This causes the '\s+$' regex to perform HORRIBLY. If you want to see why, try this: perl -mre=debug -e '"abc def ghi jkl mno" =~ /\s+$/' Notice that the string "abc def... mno" doesn't end in whitespace, but that the engine checks for /\s+$/ at each chunk of whitespace. Icky awful. -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ what does y/// stand for? why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: remove blanks
This is the correct reply. That's some good stuff. can you run a benchmark against these, contributed by others on this list, as well? $username =~ s/^\s*(.*?)\s*$/$1/; s/^\s+//, s/\s+$// $username; thanks, -rkl > I ran some benchmarks. > > The two-liner outperformed the one-liners by a 10 to 1 ratio. Code and > results below. > > > Benchmark: timing 10 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] > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: remove blanks
That's some good stuff. can you run a benchmark against these, contributed by others on this list, as well? $username =~ s/^\s*(.*?)\s*$/$1/; $username =~ s/^\s*(.*?)\s*$/$1/; thanks, -rkl > I ran some benchmarks. > > The two-liner outperformed the one-liners by a 10 to 1 ratio. Code and > results below. > > > Benchmark: timing 10 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] > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: remove blanks
this looks convenience thanks, -rkl > [EMAIL PROTECTED] wrote: >> 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! > > Doing it in two steps is the way to go. Don't try to make one regex out of > it. > > I usually write it this way, so I can process several variables at once: > >s/^\s+//, s/\s+$// for $foo, $bar, $baz; > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: remove blanks
[EMAIL PROTECTED] wrote: > 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! Doing it in two steps is the way to go. Don't try to make one regex out of it. I usually write it this way, so I can process several variables at once: s/^\s+//, s/\s+$// for $foo, $bar, $baz; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: remove blanks
I ran some benchmarks. The two-liner outperformed the one-liners by a 10 to 1 ratio. Code and results below. Benchmark: timing 10 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]
Re: remove blanks
On Monday, September 29, 2003, at 07:04 PM, [EMAIL PROTECTED] wrote: Is there a func or a onliner for removing blanks from both ends? I'm using these: $username =~ s/^\s+//; $username =~ s/\s+$//; We could combine those: $username =~ s/^\s*(.*?)\s*$/$1/; Hope that helps. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: remove blanks
That is what you want to use. You could do it in a single regex: ## NOT RECOMMENDED - SEE NOTE BELOW ## $username =~ s/^\s+|\s+$//g; The downside is that this is not efficient and actually takes Perl longer to perform the operation. If you want to know why you need to know a little about how Perl stores strings internally. Rob -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]
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]