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
|