use 5.16.2; #use warnings; use Test::More; use Benchmark qw( cmpthese ); # Returns non-empty strings with leading and trailing spaces removed. sub mca # McA { grep { $_ ne "" } map { s/^\s+//; s/\s+$//; $_ } @_; } sub tux # Tux { map { defined $_ ? m/(\S(?:.*\S)?)/ : () } @_; } sub choroba # choroba { grep !m/^$/ => map s/^\s+|\s+$//gr => @_ } sub eplam # eyepopslikeamosquito { grep { !m/^\s*$/ && s/^\s*|\s*$//g } @_; } my @x = (" hello ", "\thello again\t", "", " ", undef, " \t", "jock", "\t abc", "def \t "); my @e = ("hello", "hello again", "jock", "abc", "def"); is_deeply ([ mca (@x) ], \@e, "mca"); is_deeply ([ tux (@x) ], \@e, "tux"); is_deeply ([ choroba (@x) ], \@e, "choroba"); is_deeply ([ eplam (@x) ], \@e, "eplam"); cmpthese (1000000, { "mca" => sub { my @y = mca (@x); }, "tux" => sub { my @y = tux (@x); }, "choroba" => sub { my @y = choroba (@x); }, "eplam" => sub { my @y = eplam (@x); }, }); done_testing; __END__ ok 1 - mca ok 2 - tux ok 3 - choroba ok 4 - eplam Rate eplam choroba mca tux eplam 100908/s -- -40% -57% -66% choroba 169205/s 68% -- -29% -43% mca 236967/s 135% 40% -- -20% tux 297619/s 195% 76% 26% -- 1..4