Question on printing an array

D

Dr.Ruud

Uri Guttman schreef:
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__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,186
Messages
2,570,998
Members
47,587
Latest member
JohnetteTa

Latest Threads

Top