use Benchmark; my @words = ('trim_unneeded',' front trim only','rear trim only ',' both side trim '); for my $word (@words){ print "Benchmarking $word...\n\n"; timethese(1_000_000, {double => sub{ $word =~ s/^\s+//; $word =~ s/\s+$//; }, single => sub{ $word =~ s/^\s+|\s+$//; }}) }