这是我要优化的子程序。它大部分使用数组引用。目前这个子程序大约需要。平均运行 30-40 秒。如果可能的话,我想将其减少到 10 秒。你有没有看到任何不必要的东西突然出现在你面前?
sub compute{
# takes two params: 2 array_refs
my ($gene_exp_ref, $centroids_ref) = @_;
my ($numerator, $denominator) = 0;
my ($prod_ref, $diff_x_ref, $diff_y_ref, $x_sq_ref, $y_sq_ref) = []; # diff_y is the center_gene
my %gene_center_pcc; # diff_x is gene of interest
my $gene_exp_average = mean($gene_exp_ref);
for my $gene_exp (@{$gene_exp_ref}) {
push(@{ $diff_x_ref }, ($gene_exp - $gene_exp_average));
}
# possible bottleneck
for my $centroid_gene_exp_ref (values %{$centroids_ref}){
$diff_y_ref = []; # initilize back to empty array
for my $index (@{$centroid_gene_exp_ref}) {
push(@{ $diff_y_ref }, ($index - mean($centroid_gene_exp_ref)));
}
@{ $prod_ref } = map { @{ $diff_x_ref }[$_] * @{ $diff_y_ref }[$_] } 0..$#{ $diff_x_ref };
$numerator = sum($prod_ref);
@{ $x_sq_ref } = map {$_*$_}@$diff_x_ref;
@{ $y_sq_ref } = map {$_*$_}@$diff_y_ref;
$denominator = sqrt(sum($x_sq_ref)) * sqrt(sum($y_sq_ref));
my $r = $numerator/$denominator;
my ($center) = grep { @{$gene_centers{$_}} ~~ @$centroid_gene_exp_ref } keys %gene_centers;
$gene_center_pcc{$center} = $r;
}
#return the center with the highest PCC
return (sort {$gene_center_pcc{$b} <=> $gene_center_pcc{$a}}
keys %gene_center_pcc)[0];
}
每个计算和数字运算步骤都是必要的。它可以编译,但除非您有数据文件,否则您将无法正确使用子例程。