http://www.perlmonks.org?node_id=467807


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?

Smylers