use Benchmark 'cmpthese'; use Regexp::Common 'whitespace'; my $f = ' this is a string with spaces to remove '; cmpthese(-3, { 'Regexp-Common' => sub { $_=$f; s/$RE{ws}{crop}//g; }, 'two-s///' => sub { $_=$f; s/^\s+//; s/\s+$//; }, 'one-s///' => sub { $_=$f; s/^\s+|\s+$//g; }, 's-capture' => sub { $_=$f; s/^\s*(.*?)\s*$/$1/; }, 's-capture2' => sub { $_=$f; s/^\s*(\S+(?:\s+\S+)?)?\s*$/$1/; }, 'm-capture' => sub { $_=$f; ($_) = /(\S+(?:\s+\S+)?)/; }, });