Some non-issue stuff for a start. First, always use strictures (use strict; use warnings;). In this case you might discover that the chomps are chomping on $_ and not the loop variables I suspect you think they are chomping on. However you would be much better to chomp the whole slurped array rather than performing @input_data + @input_data2 chomps:

chomp @input_data;

There seems to be a missing ++$record_count; (I assume following the outer loop print).

Some advantage can be gained by pre-splitting the lines. Consider:

use strict; use warnings; use Benchmark qw(cmpthese); my @data = <DATA>; cmpthese (-1, { original => sub {original (@data)}, presplit => sub {presplit (@data)}, }); sub original { my @input_data = @_; my $record_count = 0; # Loop through the array for my $element (@input_data) { chomp $element; my @first = split(",", $element ) ; my $jcount = 0; my @result; # Loop through the array again for my $inside_elem (@input_data) { chomp $inside_elem; my @second = split(",", $inside_elem) ; # Build only elements below the diagonal last if ( $jcount++ > $record_count ) ; # Covariance logic # No issues with the logic # sum of products of corresponding elements in the arrays my $sum = 0; my $count = 0; for (@first) { $sum += $_ * $second[$count++] ; } $sum /= scalar(@first) ; push @result, $sum ; } my $str_to_write = join(", ", @result)."\n" ; undef @result ; # Open the output file handler in append mode. #print $str_to_write; ++$record_count; } } sub presplit { my @input_data = @_; my $record_count = 0; chomp @input_data; @input_data = map {[split ',']} @input_data; # Loop through the array for my $first (@input_data) { my $jcount = 0; my @result; # Loop through the array again for my $second (@input_data) { # Build only elements below the diagonal last if $jcount++ > $record_count; # Covariance logic # No issues with the logic # sum of products of corresponding elements in the arrays my $sum = 0; my $count = 0; $sum += $_ * $second->[$count++] for @$first; $sum /= scalar @$first; push @result, $sum ; } my $str_to_write = join(", ", @result)."\n" ; # Open the output file handler in append mode. #print $str_to_write; ++$record_count; } } __DATA__ 0.2939383839, -0.0929288282, 0.2939383839, -0.0929288282, -0.092928828 +2, 0.2939383839, -0.0929288282 0.0139383839, -0.1929288282, 0.0139383839, -0.1929288282, -0.192928828 +2, 0.0139383839, -0.1929288282 0.2009383839, 0.0929288282, 0.2009383839, 0.0929288282, 0.092928828 +2, 0.2009383839, 0.0929288282 0.0939383839, 0.5929288282, 0.0939383839, 0.5929288282, 0.592928828 +2, 0.0939383839, 0.5929288282 0.2939383839, -0.0929288282, 0.2939383839, -0.0929288282, -0.092928828 +2, 0.2939383839, -0.0929288282 0.0139383839, -0.1929288282, 0.0139383839, -0.1929288282, -0.192928828 +2, 0.0139383839, -0.1929288282 0.2009383839, 0.0929288282, 0.2009383839, 0.0929288282, 0.092928828 +2, 0.2009383839, 0.0929288282 0.0939383839, 0.5929288282, 0.0939383839, 0.5929288282, 0.592928828 +2, 0.0939383839, 0.5929288282 0.2939383839, -0.0929288282, 0.2939383839, -0.0929288282, -0.092928828 +2, 0.2939383839, -0.0929288282 0.0139383839, -0.1929288282, 0.0139383839, -0.1929288282, -0.192928828 +2, 0.0139383839, -0.1929288282 0.2009383839, 0.0929288282, 0.2009383839, 0.0929288282, 0.092928828 +2, 0.2009383839, 0.0929288282 0.0939383839, 0.5929288282, 0.0939383839, 0.5929288282, 0.592928828 +2, 0.0939383839, 0.5929288282 0.2939383839, -0.0929288282, 0.2939383839, -0.0929288282, -0.092928828 +2, 0.2939383839, -0.0929288282 0.0139383839, -0.1929288282, 0.0139383839, -0.1929288282, -0.192928828 +2, 0.0139383839, -0.1929288282 0.2009383839, 0.0929288282, 0.2009383839, 0.0929288282, 0.092928828 +2, 0.2009383839, 0.0929288282 0.0939383839, 0.5929288282, 0.0939383839, 0.5929288282, 0.592928828 +2, 0.0939383839, 0.5929288282 0.2939383839, -0.0929288282, 0.2939383839, -0.0929288282, -0.092928828 +2, 0.2939383839, -0.0929288282

Prints:

Rate original presplit original 205/s -- -59% presplit 498/s 143% --

DWIM is Perl's answer to Gödel

In reply to Re: Issue on covariance calculation by GrandFather
in thread Issue on covariance calculation by Mandrake

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.