1 Replies - 474 Views - Last Post: 09 October 2009 - 02:41 AM 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

code timing out need to improve execution time

Posted 09 October 2009 - 02:34 AM

so i have this bit of code
function engageMission(){// attacks the mission returns an output  
		$troops = $_POST['troopsEngage'];
		$output = "";
		$totpower = 0;
		$totStats = array(0, 0, 0, 0, 0, 0);
		for($x=0;$x<count($troops);$x++){					
			$y = array_search($troops[$x], $this->troop_types[$this->fort['tech_age']]);
			$output .= $this->fort[$this->db_troops[$y]]." ".$troops[$x]."s<br>";
			$totpower += $this->getTroopPower($y, $this->id, $this->fort[$this->db_troops[$y]]);
				for($z=0;$z<7;$z++){
					$array = $this->troop_stats[$this->fort['tech_age']];
					$array1 = $array[$y];
					$totStats[$z] += $array1[$z]*$this->fort[$this->db_troops[$y]];// total stats for all troops 
				}			
		}
		$missionId = $_GET['f'];
		$this->getMission($missionId);
		$power = $this->getMissionPower();
		$troops = $this->troop_types[$this->fort['tech_age']];
		for($i=0;$i<6;$i++){
			$iPower[] = $this->getTroopPower($i, $this->id, 1);			
		}	
		
		$output .= "Total power engaged = ".$totpower."<br> This power consists of :<br>";
		for($a=0;$a<7;$a++){
			$output .= $totStats[$a]." ".$this->troopStatsNamesShow[$a]."<br>";
		}
		
		while($engagePower < ($power-250)){
			$x = rand(0, 5);
			while($x != 3){
				$this->engagePower[$x]++;
				$engagePower += $iPower[$x];
			}
		}
		
		echo $output;
		echo $power;
		echo $engagePower;
		for($x=0;$x<6;$x++){
			$array = $this->troop_types[$this->fort['tech_age']];
			echo $this->engagePower[$x]." ".$array[$x];
		}
		
	}


which was working fine till i added this bit of code

while($engagePower < ($power-250)){
			$x = rand(0, 5);
			while($x != 3){
				$this->engagePower[$x]++;
				$engagePower += $iPower[$x];
			}
		}
		


now as far as i can tell that shouldnt take long. but its taking 30seconds + and timing out with a fatal error.

how can i improve this to make it quicker

what i need is a random number between 0 and 5 inclusive but not 3 which i can use to get the power of that troop and add it to the engage power. when the engagePower is greater that the mission power minus 250 then stop executing and show which troops were selected.


the line which it is saying is bad is this line -> $this->troopEngage[$x]++; (thought it was $this->engagePower[$x]++; but it wasnt so i changed it and still no luck)

This post has been edited by ghqwerty: 09 October 2009 - 02:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: code timing out need to improve execution time

#2 ghqwerty  Icon User is offline

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

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

Re: code timing out need to improve execution time

Posted 09 October 2009 - 02:41 AM

fixed it by changing the second while loop to an if statement. lol my bad
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1