Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^6: Converting multiple spaces to nbsp

by ikegami (Patriarch)
on Jun 17, 2005 at 15:48 UTC ( [id://467777]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Converting multiple spaces to nbsp
in thread Converting multiple spaces to nbsp

Actually, I'd probably go with my own variant (see above), which happens to be faster than either of these.

Yours isn't the fastest for me, although I've added use warnings, use strict; and forced a scalar context unto the substitution:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my $test_text = q|Wow, that was quick!<br/> Two points:<br/> 1) I only want space, not tabs or new lines - so shouldn't the \s be + replaced with " "? <br/> 2) Is there a difference between inkgmi's and GrandFather's entry? <b +r/> PS I thought executed regexs are experimental (so says the man page) - + is there a problem with them?<br/> |;
cmpthese(-3, { GrandFather => sub { local $_ = $test_text; scalar s/ ( +)/" " . ("&nbsp;" x length ($1))/ge }, ikegami => sub { local $_ = $test_text; scalar s/(?<= )( +)/'&nbsp;' x length($1)/eg }, Smylers => sub { local $_ = $test_text; scalar s/(?<= )( )/&nbsp;/g }, }); __END__ Rate ikegami Smylers GrandFather ikegami 22180/s -- -25% -31% Smylers 29772/s 34% -- -8% GrandFather 32268/s 45% 8% --

Replies are listed 'Best First'.
Re^7: Converting multiple spaces to nbsp
by Smylers (Pilgrim) on Jun 17, 2005 at 16:55 UTC
    Yours isn't the fastest for me,

    I can reproduce your results with your benchmark. So I compared it with mine and played 'spot the difference'. It seems that the significant difference was in the source text, which I'd copied from the original without thinking about it very much.

    As it happens, the way I'd copied the code and indented it in my editor I had $text_text wrapped on to 7 lines, and with a couple of spaces indenting the 2nd and subsequent lines. This gives several more places in the text where substitutions are required.

    So for comparison, with your benchmark above I get:

    Rate ikegami Smylers GrandFather ikegami 17125/s -- -28% -34% Smylers 23635/s 38% -- -9% GrandFather 25951/s 52% 10% --

    Putting 2 spaces at the beginning of the lines gives:

    Rate ikegami GrandFather Smylers ikegami 10990/s -- -20% -37% GrandFather 13676/s 24% -- -22% Smylers 17494/s 59% 28% --

    So vastly increasing the number of places where substitutions are required, by running s/e/e  e/g over the source text, gives:

    ikegami 3615/s -- -3% -50% GrandFather 3711/s 3% -- -48% Smylers 7181/s 99% 94% --

    But, reverting those extra double-spaces back out of the source text and instead increasing its indentation to 10 spaces at the beginning of each line gives these results:

    Rate Smylers ikegami GrandFather Smylers 6692/s -- -33% -43% ikegami 9917/s 48% -- -16% GrandFather 11769/s 76% 19% --

    Note that for the /e-based solutions there are the same number of places that need substitutions as when the indent was just by 2 characters, but for my solution the number of substitutions has greatly increased, cos each one is just of a single character.

    It seems reasonable that a substitution using /e takes longer than one with out, so avoiding using /e has greater benefit the more substitutions that are required. But yours and GrandFather's methods which replace a sequence of spaces in one go have greater benefit the longer the sequences of spaces are.

    So which method is faster depends on whether you expect there to be many places that need substituting, and whether you expect them only to be a couple of spaces long or much longer.

    Does that make sense?

    Smylers

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found