OK, Paul. As you obviously don't believe me. Here goes...
I said:
$x = "12345123451234512345x456456456456y8989";
$x =~ s/\d+//g; # faster than s/\d//g
Can you see why?
And here's my reasoning:
If you use s/\d//g on that string, you will do 36 match-and-replace's.
If you use s/\d+//g, you do 3 match-and-replace's.
Running a benchmark...
Code:
wil@localhost [12:20pm] ~ #130> bleadperl -MBenchmark=cmpthese
cmpthese(-5, {
fast => sub {
my $x = "12345123451234512345x456456456456y8989";
$x =~ s/\d+//g;
},
slow => sub {
my $x = "12345123451234512345x456456456456y8989";
$x =~ s/\d//g;
}
});
__END__
Benchmark: running fast, slow for at least 5 CPU seconds...
fast: 48872.57/s (n=256581)
slow: 11055.86/s (n=60365)
Rate slow fast
slow 11056/s -- -77%
fast 48873/s 342% --
These results prove to you how much faster using
\d+ is.
- wil