I have a feeling I may be about to learn something... ;-)
use strict;
use warnings;
use Benchmark qw( cmpthese );
my $results = cmpthese( -10, {
'r3' => sub {
my $string = " stuff ";
$string =~ s/^\s//g;
$string =~ s/\s$//g;
$string =~ s/\s+$//g;
},
'r1' => sub {
my $string = " stuff ";
$string =~ s/^\s|\s$|\s+$//g;
},
}
);
Rate r1 r3
r1 295926/s -- -1%
r3 299980/s 1% --
Updated: Now that I see GrandFather's detailed Benchmark below, I see that my error was to use a short string. When I change to
my $string = (' ' x 1000) . 'x' . (' ' x 1000);
matching GrandFather's, I get the following:
Rate r3 r1
r3 61890/s -- -79%
r1 297607/s 381% --
So Lies, damn lies, and benchmarks (with the wrong data), indeed.
|