#!/usr/bin/perl use strict; use warnings; use Benchmark qw( cmpthese timethese ); sub trim1 { @_ = $_ if not @_ and defined wantarray; @_ = @_ if defined wantarray; for ( @_ ? @_ : $_ ) { s/^\s+//, s/\s+$// } return wantarray ? @_ : $_[ 0 ] if defined wantarray; } sub trim2 { return trim2( $_ ) if not @_; return map { local $_ = $_; s/^\s+//, s/\s+$//; $_ } @_ if defined wantarray; for ( @_ ) { s/^\s+//, s/\s+$// } } my $cpu = -1; cmpthese timethese $cpu => { undef_default_1 => sub { $_ = ' asdf '; trim1(); }, undef_default_2 => sub { $_ = ' asdf '; trim2(); }, }; cmpthese timethese $cpu => { scalar_default_1 => sub { $_ = ' asdf '; my $n = trim1(); }, scalar_default_2 => sub { $_ = ' asdf '; my $n = trim2(); }, }; cmpthese timethese $cpu => { scalar_passed_1 => sub { my $x = ' asdf '; my $n = trim1( $x ); }, scalar_passed_2 => sub { my $x = ' asdf '; my $n = trim2( $x ); }, }; cmpthese timethese $cpu => { list_default_1 => sub { $_ = ' asdf '; my @n = trim1(); }, list_default_2 => sub { $_ = ' asdf '; my @n = trim2(); }, }; cmpthese timethese $cpu => { list_passed_1 => sub { my @l = ( ' asdf ', ' asdf ' ); my @n = trim1( @l ); }, list_passed_2 => sub { my @l = ( ' asdf ', ' asdf ' ); my @n = trim2( @l ); }, }; cmpthese timethese $cpu => { undef_passed_1 => sub { my @l = ( ' asdf ', ' asdf ' ); trim1( @l ); }, undef_passed_2 => sub { my @l = ( ' asdf ', ' asdf ' ); trim2( @l ); }, };