Thanks again everyone for the replies. If any one casre here's some benchmark info I
found out about our discussion ::
Intersting what i found. If the sting is simple they're about even.
If the string is complicated( has more matches/substitutions) it takes a and b the
same ( probably due to having to split ans join it )cd and d very close but d slightly
faster usually.
Here's the results on a string that has one substition per character searched for.
Benchmark: timing 100000 iterations of Substitute and return test -a-...
Substitute and return test -a-: 3 wallclock secs ( 2.61 usr + 0.01 sys = 2.62 CPU)
Benchmark: timing 100000 iterations of Substitute and return test -b-...
Substitute and return test -b-: 3 wallclock secs ( 2.60 usr + 0.00 sys = 2.60 CPU)
Benchmark: timing 100000 iterations of Substitute and return test -c-...
Substitute and return test -c-: 2 wallclock secs ( 1.79 usr + 0.00 sys = 1.79 CPU)
Benchmark: timing 100000 iterations of Substitute and return test -d-...
Substitute and return test -d-: 2 wallclock secs ( 1.78 usr + 0.00 sys = 1.78 CPU)
Granted this is 100,000 times each so if you ( and I am ) using it twice then any of
the ways isn't bad time wise.
./test.pl
with no args it modifis a string that has one substitution of each thing looked for
./test.pl string_to_run
#!/usr/bin/perl
use Benchmark;
@q = ('a','b','c','d');
if($ARGV[0]) { $var = $ARGV[0]; }
else { $var = "<sdnlkhdfkjdfvkjdfnvjkd>\nhi\r "; }
print "\n";
foreach $l(@q) {
timethese(100000, {
"Substitute and return test -$l-" => "$l(\$var)",
});
print "\n";
}
sub a { join '', split /^\<|\>$|\n|\r|\s$/, $_[0] }
sub b { join '', split /^\<|\>$|\n|\r|\s$/, $_[0]; }
sub c { $_[0] =~ s/^\<|\>$|\n|\r|\s$//g ? return $_[0] : return $_[0]; }
sub d { $_[0] =~ s/^\<|\>$|\n|\r|\s$//g;return $_[0]; }