Not any more. This new smack is a lot faster by eliminating the alternation.

#!/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; $a =~ s/^\cH+//; 1 while ($a =~ s/[^\cH]\cH//g); $a; } cmpthese(-5,{uk=>\&uk,new1=>\&new1,smack=>\&smack}); $s x= 100; cmpthese(-5,{uk=>\&uk,new1=>\&new1,smack=>\&smack}); Benchmark: running new1, smack, uk, each for at least 5 CPU seconds... new1: 6 wallclock secs ( 5.39 usr + 0.03 sys = 5.42 CPU) @ 11 +344.28/s (n=61486) smack: 6 wallclock secs ( 5.24 usr + 0.02 sys = 5.26 CPU) @ 36 +814.64/s (n=193645) uk: 4 wallclock secs ( 5.26 usr + 0.06 sys = 5.32 CPU) @ 13 +261.09/s (n=70549) Rate new1 uk smack new1 11344/s -- -14% -69% uk 13261/s 17% -- -64% smack 36815/s 225% 178% -- Benchmark: running new1, smack, uk, each for at least 5 CPU seconds... new1: 5 wallclock secs ( 5.22 usr + 0.05 sys = 5.27 CPU) @ 14 +0.04/s (n=738) smack: 6 wallclock secs ( 5.26 usr + 0.07 sys = 5.33 CPU) @ 87 +3.55/s (n=4656) uk: 5 wallclock secs ( 5.18 usr + 0.04 sys = 5.22 CPU) @ 17 +6.44/s (n=921) Rate new1 uk smack new1 140/s -- -21% -84% uk 176/s 26% -- -80% smack 874/s 524% 395% --

/-\


In reply to Re: Re: Re: Re: Re: should this backspace removal code be done better? by eyepopslikeamosquito
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.