in reply to Fast Matrix Load

I'm not sure if this is fastest but it is the shortest and sweetest way. It uses perl style code rather than C style.

Update

This is 150% faster than your C style method, see this

use Data::Dumper; while(<DATA>){ push @matrix, [split/\s+/]; } print Dumper(\@matrix); __DATA__ 1 2 3 4 5 6 7 8 9 10 11 12

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Fast Matrix Load
by blakem (Monsignor) on Feb 17, 2002 at 13:09 UTC
    shortest??? are you sure? ;-)
    @matrix=map[split],<DATA>;

    -Blake

      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