OK not shortest :-/ but fastest :-), vis:
print "Writing file...\n";
open FILE, ">test.txt" or die $!;
my $line = join "\t", (1..1000);
print FILE $line, "\n" for (1..600);
close FILE;
print "File written!\n";
# Original method
open PROFILES, "<test.txt" or die $!;
my @matrix = ();
my $start = time;
while (<PROFILES>) {
@profile = split (/\t/, $_);
while ($j <= $#profile) {
$matrix[$j]->[$i] = $profile[$i];
$j++;
}
$i++;
$j = 0;
}
print "Original method takes ", time-$start, " seconds\n";
close PROFILES;
# my method
open PROFILES, "<test.txt" or die $!;
@matrix = ();
$start = time;
while(<PROFILES>){
push @matrix, [split"\t"];
}
print "My method takes ", time-$start, " seconds\n";
close PROFILES;
# Blakem's method
open PROFILES, "<test.txt" or die $!;
@matrix = ();
$start = time;
@matrix = map[split],<PROFILES>;
print "Blakem's method takes ", time-$start, " seconds\n";
close PROFILES;
__DATA__
C:\>perl matrix.pl
Writing file...
File written!
Original method takes 18 seconds
My method takes 7 seconds
Blakem's method takes 52 seconds
PS I slightly modified the original code to remove the infinite loop so it actually will work to test it.
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|