Yes, you're right, with many consecutive quotes, s/"+//g would be considerably faster than s/"//g.

Here are a few benchmarks for those of you who like performance tuning for the sake of performance tuning... :)

As can be seen easily, in the extreme (unrealistic) case with 999 consecutive quotes followed by a single other character, s/"+//g clearly wins, while with more realistic cases, the difference between the s/// variants becomes less pronounced, and tr/"//d is outperforming both of them.

#!/usr/bin/perl use strict; use warnings; no warnings "numeric" ; use Benchmark "cmpthese"; my %strings = ( '999:1' => scalar ('"' x 999 . '-') x 1000, '9:1' => '"""""""""-' x 100000, '1:1' => '"-' x 500000, ); for my $test (sort {$b<=>$a} keys %strings) { my $s = $strings{$test}; printf "\n%s... ($test):\n\n", substr($s,0,30); cmpthese (1000, { 's/"//g' => sub { my $t = $s; $t =~ s/"//g; }, 's/"+//g' => sub { my $t = $s; $t =~ s/"+//g; }, 'tr/"//d' => sub { my $t = $s; $t =~ tr/"//d; }, } ); }

___

$ ./700801.pl """"""""""""""""""""""""""""""... (999:1): Rate s/"//g tr/"//d s/"+//g s/"//g 4.09/s -- -98% -99% tr/"//d 212/s 5079% -- -31% s/"+//g 309/s 7445% 46% -- """""""""-"""""""""-"""""""""-... (9:1): Rate s/"//g s/"+//g tr/"//d s/"//g 4.49/s -- -85% -98% s/"+//g 30.7/s 584% -- -83% tr/"//d 181/s 3931% 490% -- "-"-"-"-"-"-"-"-"-"-"-"-"-"-"-... (1:1): Rate s/"+//g s/"//g tr/"//d s/"+//g 6.78/s -- -16% -97% s/"//g 8.05/s 19% -- -96% tr/"//d 211/s 3005% 2515% --

(results shown for "perl, v5.8.8 built for x86_64-linux-thread-multi", but v5.10.0 behaves similarly)


In reply to Re^4: eliminate quotes by almut
in thread eliminate quotes by quasimodem

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.