用户缓存解决递归在计算菲波拉契数列时的噩梦
时间:2007-12-07 来源:phpor
用户缓存解决递归在计算菲波拉契数列时的噩梦
?php
function feibolaqi($v) {
static $cache = array();
if (isset($cache[$v])) return $cache[$v];
if ($v 1) throw new Exception('错误');
if ($v = 2) return 1;
$result = feibolaqi($v - 1) + feibolaqi($v - 2);
$cache[$v] = $result;
return $result;
}
$start = gettimeofday(true);
echo feibolaqi(45) . '';
echo '耗时 ' . (gettimeofday(true) - $start);
?>
相关阅读 更多 +