To convert the other way is simpler and I'm struggling to find
alternatives to my original join solution. The only edge case I
can think of is empty array @lines. You may assume that no item
of @lines contains "\n". Benchmark program follows.
use strict;
use Benchmark;
my @lines = (
"",
"This is first test line",
"",
"This is 2nd",
"And 3rd",
""
);
sub a1 { my $y = join("\n", @lines, "") }
sub a2 { my $y = ""; $y .= $_ . "\n" for @lines }
timethese(600000, {
'a1' => \&a1,
'a2' => \&a2,
});
Results on Linux, Perl 5.8.0:
a1: 5 wallclock secs ( 3.98 usr + 0.00 sys = 3.98 CPU)
a2: 14 wallclock secs (11.23 usr + 0.02 sys = 11.25 CPU)
/-\
http://greetings.yahoo.com.au - Yahoo! Greetings
- Send some online love this Valentine's Day.