Suresh Govindachar said:
Hello,
What is a good way (with respect to speed)
to un-chomp an array?
This was the fastest way I could come up with:
$_ .= "\n"
foreach @lines;
I benchmarked it, your original, and a map-based solution, and found
it was about 30% faster than your original, and about twice as fast as
map:
Benchmark: timing 50 iterations of for_index, foreach, map...
for_index: 18 wallclock secs (17.44 usr + 0.31 sys = 17.75 CPU) @ 2.82/s (n=50)
foreach: 13 wallclock secs (12.54 usr + 0.17 sys = 12.71 CPU) @ 3.93/s (n=50)
map: 37 wallclock secs (35.93 usr + 0.55 sys = 36.48 CPU) @ 1.37/s (n=50)
#!/usr/bin/perl
use Benchmark;
our @arr = (1..100_000);
sub test1
{
my @a = @arr;
$_ .= "\n"
foreach @a;
}
sub test2
{
my @a = @arr;
for(my $i=0; $i<scalar @a; $i++)
{
$a[$i] .= "\n";
}
}
sub test3
{
my @a = @arr;
@a = map { $_."\n" } @a;
}
timethese(50, {
foreach => \&test1,
for_index => \&test2,
map => \&test3,
});
-----ScottG.