use strict; use warnings; use Benchmark qw(cmpthese); my $str = (' ' x 1000) . ('x' x 100000) . (' ' x 1000); cmpthese ( -5, { plusplus => \&plusplus, starstar => \&starstar, starplus => \&starplus, plusstar => \&plusstar, twosub => \&twosub, } ); print "\n\nString got clobbered\n" if $str !~ /^ .* $/; sub plusplus { $_ = $str; s/^\s+|\s+$//g; } sub starstar { $_ = $str; s/^\s*|\s*$//g; } sub starplus { $_ = $str; s/^\s*|\s+$//g; } sub plusstar { $_ = $str; s/^\s+|\s*$//g; } sub twosub { $_ = $str; s/^\s+//; s/\s*$//; } #### Rate starstar plusstar plusplus starplus twosub starstar 47.0/s -- -8% -25% -28% -42% plusstar 51.2/s 9% -- -18% -21% -37% plusplus 62.5/s 33% 22% -- -4% -23% starplus 65.1/s 39% 27% 4% -- -20% twosub 81.6/s 74% 59% 31% 25% --