$string =~ s/^\S+//; # assumed to remove *all* whitespaces from the beginning
$string =~ s/\S+$//; # assumed to remove *all* whitespaces from the end
$string =~ s/^\s+|\s+$//g; # /g needed here
####
#!/usr/bin/perl
# vi:ts=4 sw=4 et:
use strict;
use warnings;
use Benchmark qw( cmpthese );
my $results = cmpthese(
-3, {
'r2/g' => sub {
my $string = ( ' ' x 1000 ) . 'x' . ( ' ' . 1000 );
$string =~ s/^\s+//g;
$string =~ s/\s+$//g;
},
'r2' => sub {
my $string = ( ' ' x 1000 ) . 'x' . ( ' ' . 1000 );
$string =~ s/^\s+//;
$string =~ s/\s+$//;
},
'r1' => sub {
my $string = ( ' ' x 1000 ) . 'x' . ( ' ' . 1000 );
$string =~ s/^\s+|\s+$//g;
}
}
);
####
Rate r1 r2/g r2
r1 135605/s -- -19% -25%
r2/g 168081/s 24% -- -7%
r2 181494/s 34% 8% --