You are doing an awful lot of completely unnecessary copying of arrays.

Try this clean-compiling (unlike your posted code), but untested version, It should be substantially faster:

#! perl -slw use strict; ## Variables and Data Structures my $count = 1; my @probesArray; my %probes; my $size = 100; ## Reading the file open my $FILE, "data.txt" or die "ERROR: cannot read file $!\n"; while (my $line = <$FILE>){ chomp $line; my @line = split '\t', $line; $probes{ $line[0] } = [ @line[ 1 .. $#line ] ]; ## value of the ha +sh as an array ## correlation between 1-2 or 2-1 will be same so using calculatin +g only once can be done thru this array $probesArray[ $count ] = $line[ 0 ]; $count++; } ## Frankly speaking reading of the file takes less than 3 sec with 50, +000 categories each having 100 values close $FILE; ## Correlation Calculation for( my $i = 0; $i <= $count-1; $i++ ){ for( my $j = $i+1; $j <= $count; $j++ ){ my $cor = correlation( $probes{ $probesArray[$i] }, $probes{ $ +probesArray[$j] }, $size ); ## correlation is the subroutine $probes{ $probesArray[$i] . "-" . $probesArray[$j] } = $cor; # print $count,"\t",$probesArray[$i]."-".$probesArray[$j],"\t",$ +cor,"\n"; $count++; } } ## Subroutines ## Pearson Correlation Coefficient sub correlation { my( $arr1, $arr2, $size ) = @_; my( $mean_x, $mean_y ) = mean( $arr1, $arr2, $size ); my( $ssxx, $ssxy, $ssyy ) = ss( $arr1, $arr2, $mean_x, $mean_y ); my $cor = $ssxy / sqrt( $ssxx * $ssyy ); return $cor ; } sub mean { my( $arr1, $arr2, $size ) = @_; my $mu_x = sum( @$arr1 ) / $size; my $mu_y = sum( @$arr2 ) / $size; return($mu_x,$mu_y); } ## Sum of Squared Deviations to the mean sub ss { my( $arr1, $arr2, $mean_x, $mean_y ) = @_; my( $ssxx, $ssxy, $ssyy ) = (0) x 3; ## looping over all the samples for( my $i = 0; $i < @$arr1; $i++ ){ $ssxx = $ssxx + ( $arr1->[$i] - $mean_x )**2; $ssxy = $ssxy + ( $arr1->[$i] - $mean_x ) * ( $arr2->[$i] - $m +ean_y ); $ssyy = $ssyy + ( $arr2->[$i] - $mean_y )**2; } return ($ssxx, $ssxy, $ssyy); }

Ask questions about anything you do not understand.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Improving the Nested For Loop by BrowserUk
in thread Improving the Nested For Loop by Anonymous Monk

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.