set_time_limit does not work
Asked Answered
G

2

6

I have a bigint class in php, to calculate big numbers. It works well, except the time limit.

I set the time limit with

set_time_limit(900);

in my bigint.php file, and it works in localhost. But in my web host, when I try to calculate 999^999, it yields the error

Fatal error: Maximum execution time of 10 seconds exceeded in /home/vhosts/mysite.com/http/bigint/bigint.php on line 156

This is my code:

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()

I tried putting set_time_limit(900); in both starting of php file and the constructor of class, none worked.

I thought it was a session issue, closed my browser and re-opened the page, still it takes 10 seconds as time limit.

What am I doing wrong here?

Groat answered 27/2, 2013 at 15:5 Comment(3)
Isn't your function a very long way of writing pow($digit, $digit);?Chlortetracycline
this is just a function from class bigint. And I could not find a php way to support "numbers" more than 20 characters.Groat
Just thought I'd mention it as it might help you optimize your functions; php has got a lot of math functions included :-)Chlortetracycline
A
5

"This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini." - http://php.net/manual/en/function.set-time-limit.php

Check it.

Avram answered 27/2, 2013 at 15:8 Comment(0)
B
3

Probably your apache is configure to stop execution after 10 seconds, does your php.ini have the line:

max_execution_time = 10

if so, to disable the timeout (not recommended) change it to:

max_execution_time = 0

or to any integer limit of your choice:

set_time_limit(360);

You can read more at the php manuals. Generally it's not a good idea to set it to 0 since if for example waiting for a resource that doesn't exists the server process might get stuck.

Update

If the site is hosted than it depends on the hosting company, ask them if it's possible.

another way around it is to use an ajax call instead, to keep the client waiting instead of the server

Bernicebernie answered 27/2, 2013 at 15:8 Comment(1)
Thanks. but I am hosting this file in a web host. can I change their apache/php configuration files?Groat

© 2022 - 2024 — McMap. All rights reserved.