in reply to Re: should this backspace removal code be done better?
in thread should this backspace removal code be done better?
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% --
|
|---|