Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^7: Converting multiple spaces to nbsp

by Smylers (Pilgrim)
on Jun 17, 2005 at 16:55 UTC ( #467807=note: print w/replies, xml ) Need Help??

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

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?


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://467807]
LanX it's an imaginary list ...
LanX ... reflecting root problems
choroba uses real lists so he can easily insert as many items in between as needed
LanX questions the integerity of this approach. ..
LanX doesn't sound natural
[LanX]: welcome to a new episode of Big Perl Theory...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2018-03-20 10:30 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (250 votes). Check out past polls.