#!/usr/bin/perl use strict; use warnings; use Benchmark::Forking qw( timethese cmpthese ); # UnixOS # use Benchmark qw(:all) ; # WindowsOS my $string = q{asd" efg"}; sub Solution_1 { # Solution 1 $string =~ tr/"//d; # print $string . "\n"; # asd efg } sub Solution_2 { # Solution 2 $string =~ tr/"/'/; # print $string . "\n"; # asd efg } sub Solution_3 { # Solution 3 $string =~ s/^"(.*)"$/$1/; # print $string . "\n"; # asd efg } sub Solution_4 { # Solution 4 $string =~ s/"/'/g; # print $string . "\n"; # asd efg } my $results = timethese(10000000, { Solution_1 => \&Solution_1, Solution_2 => \&Solution_2, Solution_3 => \&Solution_3, Solution_4 => \&Solution_4, }, 'none'); cmpthese( $results ); __DATA__ $ perl test.pl Rate Solution_3 Solution_1 Solution_2 Solution_4 Solution_3 7352941/s -- -63% -65% -65% Solution_1 20000000/s 172% -- -6% -6% Solution_2 21276596/s 189% 6% -- 0% Solution_4 21276596/s 189% 6% 0% -- =comment Would anyone care to share why perl can do tr/"/'/ faster than s/"/'/? Because s/// has to be able to handle all RE syntax and specialness while tr/// just considers its input to (mostly) be a simple character class. Much easier to deal with and, thus, much faster. =cut #### #!/usr/bin/perl use strict; use warnings; sub test_subroutine { $_[0] =~ s/"//g; return $_[0]; } my $string = q{asd" efg"}; my $string_formatted = test_subroutine($string); print $string_formatted . "\n"; # asd efg