In the quest for speed I've written this code in a way I wouldn't normally but hopefully it reflects your requirement. A 100 iterations takes about a minute on my desktop so the million would take 170 hours !! - I'll work on speeding it up.
#!perl use strict; use List::Util 'shuffle'; # parameters my $size = 100_000; my $repeat = 100; my @col1=(); my @col2=(); my @col3=(); my @col4=(); my @col5=(); my @col6=(); my @col7=(); my @col8=(); my @col9=(); my @col10=(); # create a test file my $file = 'table.dat'; test_data($file,$size); # load file into arrays open IN,'<',$file or die "Could not open $file : $!"; my $total=0; while (<IN>){ chomp; my @f = split "\t",$_; push @col1,$f[1]; push @col2,$f[2]; push @col3,$f[3]; push @col4,$f[4]; push @col5,$f[5]; push @col6,$f[6]; push @col7,$f[7]; push @col8,$f[8]; push @col9,$f[9]; push @col10,$f[10]; for my $i (1..10){ $total += $f[$i]; } } print "$. lines read from $file. Total 1's = $total\n"; my @count=(); my $sum=0; my $t0 = time(); # shuffle arrays and count my @c1=(); my @c2=(); my @c3=(); my @c4=(); my @c5=(); my @c6=(); my @c7=(); my @c8=(); my @c9=(); my @c10=(); for my $n (1..$repeat){ @c1 = shuffle @col1; @c2 = shuffle @col2; @c3 = shuffle @col3; @c4 = shuffle @col4; @c5 = shuffle @col5; @c6 = shuffle @col6; @c7 = shuffle @col7; @c8 = shuffle @col8; @c9 = shuffle @col9; @c10 = shuffle @col10; for my $i (1..$size){ $sum=0; $sum += pop @c1; $sum += pop @c2; $sum += pop @c3; $sum += pop @c4; $sum += pop @c5; $sum += pop @c6; $sum += pop @c7; $sum += pop @c8; $sum += pop @c9; $sum += pop @c10; ++$count[$sum]; } } # report stats my $total=0; print "Sum Count\n"; print "--- ----------\n"; for my $i (1..10){ printf "%2d %10d\n",$i,$count[$i]; $total += $i * $count[$i]; } print "Total = $total\n"; # run time my $dur = (time() - $t0); print "$repeat repeats for $size line table took $dur secs\n"; # test file sub test_data { my ($file,$lines) = @_; open OUT,'>',$file or die "Could not open $file : $!"; for (1..$lines){ print OUT $_; for (1..10){ print OUT "\t".int rand(2); } print OUT "\n"; } close OUT; print "$lines created in $file\n"; }
poj

In reply to Re^3: Table shuffling challenge by poj
in thread Table shuffling challenge by glow_gene

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.