【发布时间】:2013-02-27 15:05:55
【问题描述】:
我在 php 中有一个 bigint 类,用于计算大数字。效果很好,除了时间限制。
我设置了时间限制
set_time_limit(900);
在我的 bigint.php 文件中,它在 localhost 中工作。 但是在我的虚拟主机中,当我尝试计算 999^999 时,会产生错误
致命错误:/home/vhosts/mysite.com/http/bigint/bigint.php 第 156 行的最大执行时间超过 10 秒
这是我的代码:
public function Multiply_Digit($digit){ //class function of bigint
if($digit==0){$this->str="0";}
else
{
$len=$this->length();
$Result = new bigint("0");
$carry=0;
$current;
/*line 156:*/ for ($i = 0; $i < $len; $i++)
{
$current = $this->str[$len-$i-1] * $digit;
if ($i == 0) { $Result->str = ""+(($current + $carry) % 10); }
else{ $Result->str .= ""+(($current + $carry) % 10); }
$carry = (($current+$carry) - ($current+$carry)%10)/10;
}
$Result->str .= ""+$carry;
$Result->str = strrev($Result->str);
$Result->TrimLeadingZeros();
$this->str = $Result->str;//sacma oldu.
}//else. digit not zero
}//Multiply_Digit()
我尝试将set_time_limit(900); 放在 php 文件的开头和类的构造函数中,都没有工作。
我以为是会话问题,关闭浏览器重新打开页面,仍然需要 10 秒作为时间限制。
我在这里做错了什么?
【问题讨论】:
-
你的函数写
pow($digit, $digit);的路不是很长吗? -
这只是 bigint 类的一个函数。而且我找不到支持超过 20 个字符的“数字”的 php 方式。
-
只是想我会提到它,因为它可能会帮助您优化功能; php 包含很多数学函数 :-)
标签: php performance time-limiting