D
Dr.Ruud
Uri Guttman schreef:
I just tested it with an array with millions of elements, and the
map-version was about 3 times slower than three other methods (that each
took about one minute).
#!/usr/bin/perl
use strict ;
use warnings ;
my $N = 2_000_000 ;
my $M = @ARGV ? $ARGV[0] : 0 ;
# 1: $\ = $, = "\n"; print @_
# 2: print map "$_\n", @_
# 3: print "$_\n" for @_
# 4: $\ = "\n"; print for @_
print "M:$M\n" ;
print "N:$N\n" ;
$_[$N-1] = $N ; # preallocation?
for my $i ( 1 .. $N ) {
$_[$i-1] = $i ;
}
if (1 == $M)
{
local ($,, $\) = ("\n", "\n") ;
print @_ ;
}
elsif (2 == $M)
{
print map "$_\n", @_ ;
}
elsif (3 == $M)
{
print "$_\n" for @_ ;
}
elsif (4 == $M)
{
local $\ = "\n" ;
print for @_ ;
}
else
{
print "#:$_[-1]\n" ;
}
__END__
Ruud:
i would doubt it. map or join is optimized for this but print is so
general purpose that who knows what it does with $, and $\
internally.
I just tested it with an array with millions of elements, and the
map-version was about 3 times slower than three other methods (that each
took about one minute).
#!/usr/bin/perl
use strict ;
use warnings ;
my $N = 2_000_000 ;
my $M = @ARGV ? $ARGV[0] : 0 ;
# 1: $\ = $, = "\n"; print @_
# 2: print map "$_\n", @_
# 3: print "$_\n" for @_
# 4: $\ = "\n"; print for @_
print "M:$M\n" ;
print "N:$N\n" ;
$_[$N-1] = $N ; # preallocation?
for my $i ( 1 .. $N ) {
$_[$i-1] = $i ;
}
if (1 == $M)
{
local ($,, $\) = ("\n", "\n") ;
print @_ ;
}
elsif (2 == $M)
{
print map "$_\n", @_ ;
}
elsif (3 == $M)
{
print "$_\n" for @_ ;
}
elsif (4 == $M)
{
local $\ = "\n" ;
print for @_ ;
}
else
{
print "#:$_[-1]\n" ;
}
__END__