A matter of style, but I don't care for do 1 while ... since you could just do a while with an empty block. Anyhow, I decided to benchmark all three ways:

#!/usr/bin/perl -w use Benchmark qw(cmpthese); $s = "\bthis is an\b correct\b\b\b usage\b"; sub uk { $a = $s; $a =~ s[(?:[^\cH]\cH|^\cH)][]g while 1+index $a, chr(8); $a; } sub new1 { $a = $s; while ($a =~ s/(?:[^\cH]\cH|^\cH+)//g) {} $a; } sub smack { $a = $s; do 1 while ($a =~ s/(?:[^\cH]\cH|^\cH+)//g); $a; } cmpthese(-5,{uk=>\&uk,new1=>\&new1,smack=>\&smack}); $s x= 100; cmpthese(-5,{uk=>\&uk,new1=>\&new1,smack=>\&smack}); __END__ Benchmark: running new1, smack, uk for at least 5 CPU seconds... new1: 5 wallclock secs ( 5.23 usr + 0.00 sys = 5.23 CPU) @ 31 +024.33/s (n=162393) smack: 6 wallclock secs ( 2.16 usr + 3.05 sys = 5.22 CPU) @ 29 +16.79/s (n=15222) uk: 5 wallclock secs ( 5.19 usr + 0.00 sys = 5.19 CPU) @ 36 +570.60/s (n=189710) Rate smack new1 uk smack 2917/s -- -91% -92% new1 31024/s 964% -- -15% uk 36571/s 1154% 18% -- Benchmark: running new1, smack, uk for at least 5 CPU seconds... new1: 6 wallclock secs ( 5.48 usr + 0.00 sys = 5.48 CPU) @ 36 +8.11/s (n=2016) smack: 6 wallclock secs ( 5.01 usr + 0.41 sys = 5.41 CPU) @ 32 +6.56/s (n=1768) uk: 5 wallclock secs ( 5.23 usr + 0.00 sys = 5.23 CPU) @ 46 +2.14/s (n=2419) Rate smack new1 uk smack 327/s -- -11% -29% new1 368/s 13% -- -20% uk 462/s 42% 26% --

For whatever reason, your version hits sys very hard. However, BrowserUK's version is the fastest of the pack.

Hope this helps.

antirice    
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1


In reply to Re: Re: Re: Re: should this backspace removal code be done better? by antirice
in thread should this backspace removal code be done better? by smackdab

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.