开发者

Optimizing a Recursive Method In PHP

开发者 https://www.devze.com 2022-12-17 09:56 出处:网络
I\'m writing a text tag parser and I\'m currently using this recursive method to create tags of n words. Is there a way that it can be done non-recursively or at least be optimized? Assume that $this-

I'm writing a text tag parser and I'm currently using this recursive method to create tags of n words. Is there a way that it can be done non-recursively or at least be optimized? Assume that $this->dataArray could be a very large array.

/**
 * 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);
        }
    }
}


See if you can use tail recursion rather than call-based recursion. Some rewriting may be required but a cursory looks says it is fine to do.

Tail recursion is great for a subset of recursive functions, and good practice to spot when loops can replace recursion, and how to rewrite.

Saying that, I don't know what the overhead inside PHP is of the call. Might just be one return-pointer type setup rather than a real stack wind.

Turns out about the same. Does PHP optimize tail recursive calls out itself?

Here is my rewrite, but beware, my brain is currently sleep deprived!

protected function compilePhrase($data, $currentIndex, $depth){
    /* Loop until break condition */
    while(true) {
        if (!empty($data)){
            if ($depth >= $this->phraseStart){
                $this->addDataCount($data, $depth);
            }
            if ($depth < $this->phraseDepth){
                $currentIndex = $currentIndex + 1;
                // A test here might be better than the !empty($data)
                // in the IF condition. Check array bounds, assuming
                // array is not threaded or anything
                $data .= ' '.$this->dataArray[$currentIndex]; 
                $depth += 1;
            }else{
               break;
            }
        }else{
           break;
        }
    }
    /* Finish up here */
    return($this->dataArray);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号