A couple of thoughts. First, the 'substr' test code doesn't return the same results as the others.

I'm also not sure that it makes sense to remove a backspace at the beginning of the line? What would that mean?

I don't think that it makes too much sense to benchmark strings much beyond a 80 chars or so. Mostly we don't type lines much longer than that, and optimising for the occasional use is pretty pointless. Actually, optimising this whole thing is pretty pointless unless we are going to write programs that simulate a person typing including their typos and corrections:) Even the slowest version is way faster than (I) can type a line of input.

Finally, if the optimisation is just for fun, a way to while away a boring Sunday afternoon, here's a version that does pretty well on the short tests, but comes in second on the longer strings.

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my $code = { index_substr => sub { # BrowserUk no regex local $_ = $::TESTSTRING; my $p=0; $p > 0 ? substr( $_, $p-1,2) : substr( $_, $p,1 ) = '' while 1 ++( $p=index $_, chr(8), $p ); "$_"; }, subst_while_index => sub { # BrowserUk local $_ = $::TESTSTRING; s{(?:[^\cH]\cH|^\cH)}{}g while 1+index $_, chr(8); $_; }, alternation_subst => sub { # smackdab local $_ = $::TESTSTRING; 1 while s/(?:[^\cH]\cH|^\cH+)//g; $_; }, noalternation_subst => sub { # smackdab local $_ = $::TESTSTRING; s/^\cH+//; 1 while s/[^\cH]\cH//g; $_; }, sexeger_while_index => sub { # shenme local $_ = reverse $::TESTSTRING; s{\cH[^\cH]|\cH$}{}g while 1+index $_, chr(8); scalar reverse $_; }, substr => sub { # Aristotle local $_ = $::TESTSTRING; while(/([\b]+)/g) { my $len = 2 * length($1); substr($_, pos() - $len, $len, ''); } $_; } }; $::TESTSTRING = "\bthis is an\b correct\b\b\b usage\b"; print "$_ : ", $code->{ $_ }(), $/ for keys %{ $code }; print "\nBenchmarking length ", length($::TESTSTRING), "\n"; cmpthese(-3, $code); $::TESTSTRING x= 100; print "\nBenchmarking length ", length($::TESTSTRING), "\n"; cmpthese(-3, $code); __END__ P:\test>junk sexeger_while_index : this is a corr usag alternation_subst : this is a corr usag subst_while_index : this is a corr usag index_substr : this is a correc usag noalternation_subst : this is a corr usag substr : Benchmarking length 30 Rate substr alternation_subst subst_while_index +sexeger_while_index noalternation_subst index_substr substr 913/s -- -73% -75% + -78% -85% -88% alternation_subst 3335/s 265% -- -9% + -21% -45% -56% subst_while_index 3656/s 301% 10% -- + -13% -40% -52% sexeger_while_index 4214/s 362% 26% 15% + -- -31% -44% noalternation_subst 6077/s 566% 82% 66% + 44% -- -20% index_substr 7565/s 729% 127% 107% + 80% 24% -- Benchmarking length 3000 Rate substr alternation_subst subst_while_index +sexeger_while_index index_substr noalternation_subst substr 10.6/s -- -78% -82% + -85% -90% -96% alternation_subst 48.5/s 357% -- -16% + -29% -56% -80% subst_while_index 57.6/s 443% 19% -- + -16% -48% -77% sexeger_while_index 68.6/s 548% 42% 19% + -- -38% -72% index_substr 110/s 942% 128% 92% + 61% -- -55% noalternation_subst 246/s 2220% 407% 327% + 258% 123% --

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.


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