-1

这是我要优化的子程序。它大部分使用数组引用。目前这个子程序大约需要。平均运行 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];
}

每个计算和数字运算步骤都是必要的。它可以编译,但除非您有数据文件,否则您将无法正确使用子例程。

4

2 回答 2

3
for my $index (@{$centroid_gene_exp_ref}) {
    push(@{ $diff_y_ref }, ($index - mean($centroid_gene_exp_ref)));
}

这将重新计算 中每个项目的平均值@{$centroid_gene_exp_ref}。如果该数组很大,它将以指数方式增加(我假设它mean()不会缓存或记忆结果,每次调用它时都会强制它循环遍历数组)。您可以通过自己缓存均值来节省大量时间:

my $mean = mean($centroid_gene_exp_ref);
for my $index (@{$centroid_gene_exp_ref}) {
    push(@{ $diff_y_ref }, ($index - $mean));
}

除此之外,请与Devel::NYTProf 联系,以找到您的实际瓶颈和目标优化。

于 2013-04-02T05:44:03.573 回答
2

您需要从大局来看,考虑到您之前的帖子,您在该帖子中表明您调用compute()了每个键%$centroids_ref

foreach my $key ( keys %HoA ) {
    compute($HoA{$key}, \%HoA);  # on the first iteration, this actually passes an aref to [1,3,3,3]
}

即使经过 Dave Sherohman 的优化,您仍然会一遍又一遍地进行大量计算(如mean)。

我的建议是你将外循环带入compute(). 然后对于 HoA 中的每个键,您可以存储您的计算并为每个键重用这些值。

sub compute{
    my ($centroids_ref) = @_;

    # precalculate these values once
    my %means;
    my %diffs;
    my %sqrts;
    foreach my $key (keys %$centroids_ref) {
        my $mean = mean($centroids_ref->{$key});
        my @diffs = map {$_ - $mean} @{$centroids_ref->{$key}};

        my @squares = map {$_ * $_} @diffs;
        my $sqrt = sqrt(sum(\@squares));

        $means{$key} = $mean;
        $diffs{$key} = \@diffs;
        $sqrts{$key} = $sqrt;
    }

    # now do the main calculations from the 'possible bottlenecks' section
    ...
}
于 2013-04-02T07:45:33.093 回答