It shouldn't really affect the efficiency much, as you're
limitting the split to a maximum of two results returned,
and so having a longer string passed to the split won't
affect it. Either way, the difference in efficiency will
be negligible
use Benchmark;
use strict;
timethese(1000000, { 'One Liner' => \&one_liner, 'Two Liner' => \&two_
+liner });
sub one_liner {
# Stick all variables in the functions, so we can
# see if there's any extra overhead for the extra
# variable in two_liner.
my $text = q/Here is some test text.
I don't really know what I'm typing here, but
does it really matter? I don't think so. I'm just
kind of hitting random keys and stuff/;
my $next_line;
($text, $next_line) = split /\n/, $text.$next_line, 2;
}
sub two_liner {
my $text = q/Here is some test text.
I don't really know what I'm typing here, but
does it really matter? I don't think so. I'm just
kind of hitting random keys and stuff/;
my($next_line, $pre_next_line);
($text, $pre_next_line) = split /\n/, $text, 2;
$next_line = $pre_next_line . $next_line;
}
Benchmark: timing 1000000 iterations of One Liner, Two Liner...
One Liner: 21 wallclock secs (19.85 usr + 0.00 sys = 19.85 CPU)
Two Liner: 22 wallclock secs (22.85 usr + 0.00 sys = 22.85 CPU)
|