use strict; use warnings; use Data::Dumper; use Benchmark qw(cmpthese); # Returns non-empty strings with leading and trailing spaces removed. sub mytrim_nonempties { grep { $_ ne '' } map { s/^\s+//; s/\s+$//; $_ } @_; } sub trim_nonempties { ## no critic (ProhibitMutatingListFunctions) grep { !/^\s*$/ && s/^\s*|\s*$//g } @_; ## use critic } my @x = ( ' hello ', "\thello again\t", '', " ", " \t", 'jock', "\t abc", "def \t " ); cmpthese(1000000, { 'myfunc' => sub { my @y = mytrim_nonempties(@x); }, 'yourfunc' => sub { my @y = trim_nonempties(@x); }, });