Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Re: Re: Craftier

by snax (Friar)
on Dec 15, 2000 at 13:57 UTC ( #46817=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Craftier
in thread Craftier

Translation is always the fastest if you can use it, at least that's my experience.

One thing to consider is that the alternatives suggested remove all whitespace -- the join/split just drops leading/trailing whitespace and squishes all "extra" in between down to one space -- provided you join on ' ' and not '' -- you need to do more than one regex/translation to accomplish the same thing.

The simplest (to read) equivalent would be

s/^\s*//; s/\s*$//; s/\s+/ /g;
The quickest alternative would likely be to use translation to squish all the white space first and then do regexes to strip the (perhaps) remaining single spaces at the beginning and end of the string:
tr/ \t/ /s; s/^ //; s/ $//;
(add extra whitespace equivalents in the translation if you want to lose carriage returns and such).

In any event, the point was that the join/split is an interesting alternative, and not all that inefficient. Lots of times when I want to strip extra whitespace speed isn't that big a deal, anyway :) Some people like to use s/^\s*|\s*$/g to strip leading and trailing whitespace but that's less efficient than doing two substitutions, so it's not always about speed.

Perhaps I can persuade japhy to present his benchmarks?

Replies are listed 'Best First'.
Re: Re: Re: Re: Craftier
by chipmunk (Parson) on Dec 15, 2000 at 19:58 UTC
    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://46817]
[ambrus]: The keyboard I bought doesn't have backlighting. I specifically had to select for that, because so many keyboards these days have lights. Why would I want lights in the keyboard? I want lights in the room, such as on the ceiling, not in the keyboard.
choroba has a small USB lamp
[ww]: ++ all (phony labels re "gaming"
[1nickt]: ambrus perhaps you know where the keys are?
[ww]: OTOH, -- for ads suggesting that being good at games is somehow eq "greatness"

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (14)
As of 2017-03-27 13:53 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.