gpt4 book ai didi

php - 优化使用大量 for 循环的 PHP 代码

转载 作者:行者123 更新时间:2023-11-30 21:21:58 25 4
gpt4 key购买 nike

我的 PHP 代码包含大量 foreach 循环,结果绝对是灾难性的。运行时间太长。

有没有替代方案。我愿意接受所有建议。

我想可能会实现一个基于闪存的客户端,并使用 Actionscript 来利用客户端的 CPU 来运行逻辑。

或者有没有办法使用 C/C++ 来处理服务器本身计算要求较高的部分并返回 PHP 结果。

以下函数被调用 1,000,000 次。

public function performEnrichmentAnalysis($geneSet) {
/**
* $mainArray is a multi dimentional array.
* EntrezID | Set (0/1) | pValue | rank
*/
$mainArray = array();
$finalArray = array();
$originalGenesScore = 0;
$randomGenesScore = 0;
$u = 0;
$EntrezID = array();
$Set = array();
$pValue = array();
$Rank = array();
$originalGenes = $geneSet->getGenes();
$memeberCount = $geneSet->getGeneCount();
$randomGenes = $this->geneExpressionData->getRandomGenes($memeberCount);
/**
* Copy the elements of original and random gene sets to main array.
*/
foreach ($originalGenes as $key => $value) {
$pVal = $this->geneExpressionData->getExpressionValue($value);
$array = array('EntrezID' => $value, 'Set' => 0, 'pValue' => $pVal, 'Rank' => 999);
array_push($mainArray, $array);
unset($array);
}
foreach ($randomGenes as $key => $value) {
$pVal = $this->geneExpressionData->getExpressionValue($value);
$array = array('EntrezID' => $value, 'Set' => 1, 'pValue' => $pVal, 'Rank' => 999);
array_push($mainArray, $array);
unset($array);
}
/**
* sort the multi dimentaional array based on p-values
*/
foreach ($mainArray as $key => $row) {
$EntrezID[$key] = $row['EntrezID'];
$Set[$key] = $row['Set'];
$pValue[$key] = $row['pValue'];
$Rank[$key] = $row['Rank'];
}
array_multisort($pValue, SORT_ASC, $mainArray);

/**
* Assign ranks to the genes
*/
for ($index = 0; $index < count($mainArray); $index++) {
$row = $mainArray[$index];
$row['Rank'] = $index + 1;
$row['Score'] = 0;
//print_r($row['Rank']);
array_push($finalArray, $row);
}

/**
* Calculate scores for each gene
*/
for ($i = 0; $i < count($finalArray); $i++) {
for ($j = $i + 1; $j < count($finalArray); $j++) {
if ($finalArray[$j]['Set'] != $finalArray[$i]['Set']) {
$finalArray[$i]['Score']++;
}
}
}

/**
* Calculate score for the entire set and get universal U and z score.
*/
for ($counter = 0; $counter < count($finalArray); $counter++) {
if ($finalArray[$counter]['Set'] == 0) {
$originalGenesScore += $finalArray[$counter]['Score'];
}
if ($finalArray[$counter]['Set'] == 1) {
$randomGenesScore += $finalArray[$counter]['Score'];
}
}

if ($originalGenesScore > $randomGenesScore) {
$u = $randomGenesScore;
} else {
$u = $originalGenesScore;
}

$zNumerator = $u - (($memeberCount * $memeberCount) / (2));
$zDenominatorSquared = ($memeberCount * $memeberCount * ($memeberCount + $memeberCount + 1)) / 12;

$z = $zNumerator / sqrt($zDenominatorSquared);

if (abs($z) > 2.303) {
$this->temp001++;
} elseif (abs($z) > 1.605) {
$this->temp005++;
} else {
$this->tempRemaining++;
}
}

最佳答案

嗯,这确实不是一个可以轻易回答的问题。是的,假设您了解 C++,则可以用 C++ 重写代码。我认为这没有任何重大障碍。

您必须想出一种方法将原始 geneSet 转换为 C++ 代码,然后构造一个结构来表示您的 $mainArray 和其他 .使用 std::vector 作为数组。

由于此代码本身并不实际生成任何 Web 内容,因此您可以轻松地通过调用安装在服务器上的 C++ 程序来替换该函数。我建议您实际上在 C++ 中实现调用此函数的功能,因为如果调用此函数 1M 次,那么您最好在 C++ 中执行 1M 次调用,而不是调用一段 C++ 代码 1M 次。

关于php - 优化使用大量 for 循环的 PHP 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15179533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com