use strict; use warnings; use Benchmark q{cmpthese}; my @arr = ( q{ fdsgehw fw wwfe w } ) x 5000; cmpthese( -5, { alternation => sub { my @new = @arr; s{ ^\s* | \s*$ }{}gx for @new; }, capture => sub { my @new = @arr; s{ ^\s* (\S.*?) \s*$ }{$1}x for @new; }, splitJoin => sub { my @new = @arr; $_ = join q{ }, split for @new; }, twoStage => sub { my @new = @arr; s{ ^\s* }{}x for @new; s{ \s*$ }{}x for @new; }, twoStageComma => sub { my @new = @arr; s{ ^\s* }{}x, s{ \s*$ }{}x for @new; }, }, ); #### Rate capture alternation twoStage twoStageComma splitJoin capture 8.93/s -- -27% -51% -52% -66% alternation 12.2/s 36% -- -33% -34% -53% twoStage 18.2/s 104% 49% -- -2% -31% twoStageComma 18.6/s 108% 52% 2% -- -29% splitJoin 26.2/s 193% 115% 44% 41% --