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)
|