PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,363 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,422 people online right now. Registration is fast and FREE... Join Now!




improving execution time

 

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

ghqwerty

25 Jun, 2009 - 01:06 PM
Post #1

if($spareTime > 0){ $this->writeCode(); }
Group Icon

Joined: 8 Aug, 2008
Posts: 748



Thanked: 24 times
Dream Kudos: 25
My Contributions
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


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

User is offlineProfile CardPM
+Quote Post


RudiVisser

RE: Improving Execution Time

25 Jun, 2009 - 01:14 PM
Post #2

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
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().
User is offlineProfile CardPM
+Quote Post

ghqwerty

RE: Improving Execution Time

25 Jun, 2009 - 01:19 PM
Post #3

if($spareTime > 0){ $this->writeCode(); }
Group Icon

Joined: 8 Aug, 2008
Posts: 748



Thanked: 24 times
Dream Kudos: 25
My Contributions
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
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Improving Execution Time

25 Jun, 2009 - 01:23 PM
Post #4

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
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:
CODE
ini_set("max_execution_time", "200);


But then I remembered about set_time_limit(), just use that, forget ini_set smile.gif
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Improving Execution Time

25 Jun, 2009 - 01:31 PM
Post #5

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,065



Thanked: 151 times
Dream Kudos: 100
Expert In: PHP

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

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

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

User is offlineProfile CardPM
+Quote Post

ghqwerty

RE: Improving Execution Time

25 Jun, 2009 - 01:40 PM
Post #6

if($spareTime > 0){ $this->writeCode(); }
Group Icon

Joined: 8 Aug, 2008
Posts: 748



Thanked: 24 times
Dream Kudos: 25
My Contributions
QUOTE(CTphpnwb @ 25 Jun, 2009 - 10:31 PM) *

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

but $fort isn't defined, so I think you're causing:
CODE
        $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


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
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Improving Execution Time

25 Jun, 2009 - 01:58 PM
Post #7

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,065



Thanked: 151 times
Dream Kudos: 100
Expert In: PHP

My Contributions
Well, another problem is:
CODE
            $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:
CODE
            $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 Jun, 2009 - 02:03 PM
User is offlineProfile CardPM
+Quote Post

ghqwerty

RE: Improving Execution Time

26 Jun, 2009 - 08:11 AM
Post #8

if($spareTime > 0){ $this->writeCode(); }
Group Icon

Joined: 8 Aug, 2008
Posts: 748



Thanked: 24 times
Dream Kudos: 25
My Contributions
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.


User is offlineProfile CardPM
+Quote Post

ghqwerty

RE: Improving Execution Time

27 Jun, 2009 - 07:25 AM
Post #9

if($spareTime > 0){ $this->writeCode(); }
Group Icon

Joined: 8 Aug, 2008
Posts: 748



Thanked: 24 times
Dream Kudos: 25
My Contributions
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
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Improving Execution Time

27 Jun, 2009 - 07:42 AM
Post #10

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
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...
User is offlineProfile CardPM
+Quote Post

ghqwerty

RE: Improving Execution Time

27 Jun, 2009 - 07:43 AM
Post #11

if($spareTime > 0){ $this->writeCode(); }
Group Icon

Joined: 8 Aug, 2008
Posts: 748



Thanked: 24 times
Dream Kudos: 25
My Contributions
QUOTE(MageUK @ 27 Jun, 2009 - 04:42 PM) *

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 Jun, 2009 - 07:47 AM
User is offlineProfile CardPM
+Quote Post

ghqwerty

RE: Improving Execution Time

30 Jun, 2009 - 08:02 AM
Post #12

if($spareTime > 0){ $this->writeCode(); }
Group Icon

Joined: 8 Aug, 2008
Posts: 748



Thanked: 24 times
Dream Kudos: 25
My Contributions
bump
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Improving Execution Time

30 Jun, 2009 - 01:06 PM
Post #13

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,065



Thanked: 151 times
Dream Kudos: 100
Expert In: PHP

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

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

will ever work. That's because $fort is not an associative array.

User is offlineProfile CardPM
+Quote Post

korin43

RE: Improving Execution Time

30 Jun, 2009 - 09:38 PM
Post #14

New D.I.C Head
*

Joined: 30 Jun, 2009
Posts: 4

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


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


This post has been edited by korin43: 30 Jun, 2009 - 09:38 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:34PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month