in reply to Re: Fast Matrix Load
in thread Fast Matrix Load

shortest??? are you sure? ;-)
@matrix=map[split],<DATA>;

-Blake

Replies are listed 'Best First'.
Re: Re: Re: Fast Matrix Load
by tachyon (Chancellor) on Feb 17, 2002 at 13:39 UTC

    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

      Minor nitpick....
      @row = split;
      Will be different than:
      @row = split /\t/;
      The latter leaving the trailing newline on the last column. May or may not cause grief depending on what you do to the final matrix before trying to print it out.
      Well, I never said it was fast or memory efficient. ;-) My first golfed attempt (which had a couple extra chars over the one I posted) performs much better...
      push@matrix,[split]for<PROFILES>;

      -Blake