improving execution time

Fatal error: Maximum execution time of 30 seconds exceeded in /home/bw

Page 1 of 1

13 Replies - 849 Views - Last Post: 30 June 2009 - 10:38 PM Rate Topic: -----

#1 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

improving execution time

Posted 25 June 2009 - 02:06 PM

Fatal error: Maximum execution time of 30 seconds exceeded in /home/bwars/public_html/food.php on line 43 ok so i have this piece of code which i just made anyway i could because im quite tired and last time i tried to do something similiar it failed however now it exceeds the time limit.

now i need to improve execution time but im not relli sure WHERE i can improve it. please hep lol


<?php
require("general2.php");

class food extends general{
	var $foodeaten;
	var	$foodproduced;
	var	$differance;
	var	$endfood;	
	var $db_troops =  array("troop_a", "troop_b", "troop_c", "troop_d", "troop_e", "troop_f", "pt1", "pt2", "pt3");	
	var $crop_usage	= array(2, 2, 2, 4, 4, 8, 3, 3, 3);
	var $trooparray = array();
	var $foodmin;
	var $foodday;
	
	function update_o_fort(){// this is where i update the other players fort
		$query = "update forts set ";
		foreach($fort as $key => $value){
			if(!is_numeric($key) && $key != "fortid") { 
				$query .= $key." = '".$value."', ";
			}
		}
		$query = substr($query,0,-2)." where fortid ='".$this->ofort['fortid']."'";					
		mysql_query($query, $this->dblink) or die("Error updating forts error: ".mysql_error());
	}
	
	function sort_food(){	
		$query = "select * from forts";
		$dbdata = mysql_query($query, $this->dblink);
		$fortsa = mysql_num_rows($dbdata);
		$forts = mysql_fetch_array($dbdata);
		for($x=0;$x<$fortsa;$x++){// $x is the player
			$fort = $forts[$x];
			for($y=0;$y<9;$y++){
				$this->trooparray[] = $fort[$this->db_troops[$y]]*$fort['tech_age'];// number of troops 
				$this->foodmin[] = ($this->trooparray[$y]*$this->crop_usage[$y])/60;// food eaten per minute
			}
			$this->foodeaten = ceil(array_sum($this->foodmin));
			$this->foodproduced = $fort['production_a']/60;//per mim 
			$this->differance = $this->foodproduced - $this->foodeaten;//per min
			if($fort['quantity_a'] + $this->differance < 0){ // if food is now below 0 				
				for($a=$fort['quantity_a'];$a<5;$a+$this->foodday){
					$y = rand(0, 5);
					if($this->trooparray[$y] > 0){
						$this->trooparray[$y]--;
					}else{	
						$y = rand(6,8);
						$this->trooparray[$y]--;
					}
					$this->foodday = $this->crop_usage[$y]*$fort['tech_age']*24;//hour
					$fort['quantity_a'] = $a;
				}
			}else{
				$fort['quantity_a'] = $fort['quantity_a'] + $this->differance;
			}
			$this->update_o_fort();
		}	
	}		
}


session_start();
$food = new food;
$food->sort_food();

?>



thankyouu

Is This A Good Question/Topic? 0
  • +

Replies To: improving execution time

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: improving execution time

Posted 25 June 2009 - 02:14 PM

Depending on the amount of "food to sort", you probably can't.

I haven't looked over the code properly yet, but you could just increase max_execution_time via ini_set().
Was This Post Helpful? 0
  • +
  • -

#3 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: improving execution time

Posted 25 June 2009 - 02:19 PM

never had to use any of the ini set or whatever, could you talk me through it or send me a link to a tut
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: improving execution time

Posted 25 June 2009 - 02:23 PM

Your best friend in PHP development is php.net, php.net/functionname will give you a full description and example of usage. ini_set @ php.net.

You'd need to do:
ini_set("max_execution_time", "200);


But then I remembered about set_time_limit(), just use that, forget ini_set :)
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,796
  • Joined: 08-August 08

Re: improving execution time

Posted 25 June 2009 - 02:31 PM

Your update_o_fort() function looks funny. You have:
    foreach($fort as $key => $value){

but $fort isn't defined, so I think you're causing:
    $query = substr($query,0,-2)." where fortid ='".$this->ofort['fortid']."'";

to end up with:
$query = " where fortid ...
because
$substr($query,0,-2) = ""
Was This Post Helpful? 0
  • +
  • -

#6 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: improving execution time

Posted 25 June 2009 - 02:40 PM

View PostCTphpnwb, on 25 Jun, 2009 - 10:31 PM, said:

Your update_o_fort() function looks funny. You have:
    foreach($fort as $key => $value){

but $fort isn't defined, so I think you're causing:
    $query = substr($query,0,-2)." where fortid ='".$this->ofort['fortid']."'";

to end up with:
$query = " where fortid ...
because
$substr($query,0,-2) = ""



thanks for pointing that out (i only half changed the function) so thi is my current code


<?php
require("general2.php");

class food extends general{
	var $foodeaten;
	var	$foodproduced;
	var	$differance;
	var	$endfood;	
	var $db_troops =  array("troop_a", "troop_b", "troop_c", "troop_d", "troop_e", "troop_f", "pt1", "pt2", "pt3");	
	var $crop_usage	= array(2, 2, 2, 4, 4, 8, 3, 3, 3);
	var $trooparray = array();
	var $foodmin;
	var $foodday;
	
	function update_o_fort($fort){// this is where i update the other players fort
		$query = "update forts set ";
		foreach($fort as $key => $value){
			if(!is_numeric($key) && $key != "fortid") { 
				$query .= $key." = '".$value."', ";
			}
		}
		$query = substr($query,0,-2)." where fortid ='".$fort['fortid']."'";					
		mysql_query($query, $this->dblink) or die("Error updating forts error: ".mysql_error());
	}
	
	function sort_food(){	
		$query = "select * from forts";
		$dbdata = mysql_query($query, $this->dblink);
		$fortsa = mysql_num_rows($dbdata);
		$forts = mysql_fetch_array($dbdata);
		for($x=0;$x<$fortsa;$x++){// $x is the player
			$fort = $forts[$x];
			for($y=0;$y<9;$y++){
				$this->trooparray[] = $fort[$this->db_troops[$y]]*$fort['tech_age'];// number of troops 
				$this->foodmin[] = ($this->trooparray[$y]*$this->crop_usage[$y])/60;// food eaten per minute
			}
			$this->foodeaten = ceil(array_sum($this->foodmin));
			$this->foodproduced = $fort['production_a']/60;//per mim 
			$this->differance = $this->foodproduced - $this->foodeaten;//per min
			if($fort['quantity_a'] + $this->differance < 0){ // if food is now below 0 				
				for($a=$fort['quantity_a'];$a<5;$a+$this->foodday){
					$y = rand(0, 5);
					if($this->trooparray[$y] > 0){
						$this->trooparray[$y]--;
					}else{	
						$y = rand(6,8);
						$this->trooparray[$y]--;
					}
					$this->foodday = $this->crop_usage[$y]*$fort['tech_age']*24;//hour
					$fort['quantity_a'] = $a;
				}
			}else{
				$fort['quantity_a'] = $fort['quantity_a'] + $this->differance;
			}
			$this->update_o_fort($fort);
		}	
	}		
}


session_start();
$food = new food;
set_time_limit(60);
$food->sort_food();

?>


and it is still loading :S


oo i just got a 500 internal server error
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,796
  • Joined: 08-August 08

Re: improving execution time

Posted 25 June 2009 - 02:58 PM

Well, another problem is:
      $fort = $forts[$x];
      for($y=0;$y<9;$y++){
        $this->trooparray[] = $fort[$this->db_troops[$y]]*$fort['tech_age'];// number of troops 


$fort['tech_age'] is not defined. I think you want $forts['tech_age'].

Edit: oh, and $fort is not an array. It's a string, so you probably want something like:
			$fort = $forts[$x];
			for($y=0;$y<9;$y++){
				$this->trooparray[] = $forts[$this->db_troops[$y]]*$forts['tech_age'];// number of troops 




Most likely, something like this is causing an infinite loop, although I can't see where.

This post has been edited by CTphpnwb: 25 June 2009 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

#8 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: improving execution time

Posted 26 June 2009 - 09:11 AM

well what i need to do is loop through each row in my forts table of my db, then work out how much food they are left with after crop usage is calculated and then if it is negative kill troops untill it is positive and add on the amount of food for each killed soldier for 1 day.
Was This Post Helpful? 0
  • +
  • -

#9 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: improving execution time

Posted 27 June 2009 - 08:25 AM

any help with this subject ?? thanks for your help so far but i need to get this sorted to be honest.

the problem with just looping through the queris where fortid = $x is that if someone seletes a fort and makes a new one then there is a gap in the table, wehats the best way to do this
Was This Post Helpful? 0
  • +
  • -

#10 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: improving execution time

Posted 27 June 2009 - 08:42 AM

Did you actually try what I said?? If you set a time limit of 5 minutes and it still times out then it's a problem with your script, if not then there's just a hell of alot of data to work with...
Was This Post Helpful? 0
  • +
  • -

#11 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: improving execution time

Posted 27 June 2009 - 08:43 AM

View PostMageUK, on 27 Jun, 2009 - 04:42 PM, said:

Did you actually try what I said?? If you set a time limit of 5 minutes and it still times out then it's a problem with your script, if not then there's just a hell of alot of data to work with...



well i tried putting it at 1 minute however i intended it to be continuos and had to stick with every one minute so having to let it run for 5 mins would not be ideal


--------

just set timeout to 300 seconds like you recommended and i get this error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@blisswars.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at www.blisswars.com Port 80

This post has been edited by ghqwerty: 27 June 2009 - 08:47 AM

Was This Post Helpful? 0
  • +
  • -

#12 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: improving execution time

Posted 30 June 2009 - 09:02 AM

bump
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,796
  • Joined: 08-August 08

Re: improving execution time

Posted 30 June 2009 - 02:06 PM

Since you're using:
		   $fort = $forts[$x];

where $x is a number, I don't think that this line:
			$this->foodproduced = $fort['production_a']/60;//per mim 

will ever work. That's because $fort is not an associative array.
Was This Post Helpful? 0
  • +
  • -

#14 korin43  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-June 09

Re: improving execution time

Posted 30 June 2009 - 10:38 PM

Change this section:
$forts = mysql_fetch_array($dbdata);
		for($x=0;$x<$fortsa;$x++){// $x is the player
			$fort = $forts[$x];


To this:
while($fort = mysql_fetch_assoc($dbdata)){


This post has been edited by korin43: 30 June 2009 - 10:38 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1