Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re: Re: Re: Craftier

by chipmunk (Parson)
on Dec 15, 2000 at 19:58 UTC ( #46830=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Re: Craftier
in thread Craftier

Ah, right, the original goal was not to remove all whitespace, but to compress whitespace. Here's a new benchmark:

#!perl use Benchmark; my $junk = ' The quick brown fox Jumped over the lazy dog '; timethese(-10, { 'split' => sub { $x = join ' ', split ' ', $junk; }, 'trans' => sub { ($x = $junk) =~ tr/ \t\r\n/ /s; $x =~ s/^ //; $x =~ s/ $//; }, 'subst' => sub { ($x = $junk) =~ s/\s+/ /g; $x =~ s/^ //; $x =~ s/ $//; }, 'subst2' => sub { ($x = $junk) =~ s/^\s+//; $x =~ s/\s+$//; $x =~ s/\s+/ /g; }, });
And the results:
Benchmark: running split, subst, subst2, trans, each for at least 10 CPU seconds... split: 41225.50/s subst: 40796.61/s subst2: 38222.28/s trans: 72880.42/s
split fares much better when the task is to compress whitespace, beating either substitution solution, but translate is still the winner. The extra substitutions to remove whitespace at the beginning and the end of the string slow down quite a bit the solutions which require them.


Comment on Re: Re: Re: Re: Craftier
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://46830]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (16)
As of 2014-08-27 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (239 votes), past polls