well it appears it's the slower of the two...
#!/usr/bin/perl 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/> |; my $working_var; my $count = 1000000; cmpthese($count, { 'grandfather' => sub {$working_var = $test_text; $working_var =~ s +/ ( +)/" " . ("&nbsp;" x length ($1))/ge}, 'ikegami' => sub {$working_var = $test_text; $working_var =~ s +/(?<= )( )/'&nbsp;' x length($1)/eg} }); __OUTPUT__ Benchmark: timing 1000000 iterations of grandfather, ikegami... grandfather: 31 wallclock secs (31.01 usr + 0.00 sys = 31.01 CPU) @ 3 +2252.86/s (n=1000000) ikegami: 52 wallclock secs (51.26 usr + 0.00 sys = 51.26 CPU) @ 19 +506.87/s (n=1000000) Rate ikegami grandfather ikegami 19507/s -- -40% grandfather 32253/s 65% --
Personlly I'd go with GrandFather's solution even if it were the slower, on the grounds I think it'd be more readable to more people.
---
my name's not Keith, and I'm not reasonable.

In reply to Re^4: Converting multiple spaces to nbsp by reasonablekeith
in thread Converting multiple spaces to nbsp by eastcoastcoder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.