2
我正在寫一個文本標籤解析器和我目前使用這種遞歸的方法來創建的ň字標籤。有沒有辦法可以非遞歸地完成或至少進行優化?假設$ this-> dataArray可能是一個非常大的數組。優化遞歸方法在PHP
/**
* A recursive function to add phrases to the tagTracker array
* @param string $data
* @param int $currentIndex
* @param int $depth
*/
protected function compilePhrase($data, $currentIndex, $depth){
if (!empty($data)){
if ($depth >= $this->phraseStart){
$this->addDataCount($data, $depth);
}
if ($depth < $this->phraseDepth){
$currentIndex = $currentIndex + 1;
//$this->dataArray is an array containing all words in the text
$data .= ' '.$this->dataArray[$currentIndex];
$depth += 1;
$this->compilePhrase($data, $currentIndex, $depth);
}
}
}
嗯。如果爲第二個嵌套的if語句添加else/break,它將起作用。但是,它似乎與遞歸函數的平均速度大致相同。 – VirtuosiMedia 2010-01-21 05:58:15
很酷,會編輯。 – 2010-01-21 05:59:39
尾遞歸通常不被視爲速度優化(儘管展開堆棧確實需要時間,但與所有正在進行的字符串連接相比,它應該很小)。這是一個空間優化,以防止您溢出堆棧。 – 2010-01-21 19:56:13