|Perl Monk, Perl Meditation|
Re^7: Converting multiple spaces to nbspby Smylers (Pilgrim)
|on Jun 17, 2005 at 16:55 UTC||Need Help??|
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:
Putting 2 spaces at the beginning of the lines gives:
So vastly increasing the number of places where substitutions are required, by running s/e/e e/g over the source text, gives:
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:
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?