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)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.