Page 1 of 1

Code Optimization

Posted: Tue Mar 04, 2003 3:23 pm
by Johnm
This piece of code takes too long to execute. Does anyone have any suggestions? I have cut it down quite a bit from what the guy that wrote it had but I could use a bit of help.

Thanks,
John M

Code: Select all

<?php

  for($x=1 ; $x < 12 ; $x++)
  {
      $check=0;
      echo "<tr>\n";
      echo "<td>Position $x</td>\n";
      echo "<td><select name=pos_$x ";
      if ($x < 11)
         echo "onChange='check_lineup_pos(this.form.pos_$x, this.form.gap_$x)'";
      echo ">\n";

      echo "<option value="">&nbsp\n";
      $sql="select j.job_number 
	        from job j,job_opts o 
	        where j.modify >=".$start_am." 
              and (j.engineer="".$user_info['name']."" or j.engineer2="".$user_info['name']."") 
              and (o.type=0 or o.type=2 or o.type=4 or o.type=8 ) 
	          and o.utype < 3
              and o.job = j.job_number order by j.job_number";
	  $qry=ifx_query($sql,$dbid);
	  
	  while($row=ifx_fetch_row($qry,"next"))
      {
	      $check++;
          echo "<option>".$row['job_number']."\n"; 
	  }
      
	  if ($x < 11)
	  {
          echo "<option value=_GAP>Gap\n";
      }
      
	  echo "</select></td>\n";
      
	  if ($x < 11 && $x < $check)
	  {
          echo "<td>Gap Dim: <input type=text name=gap_$x size=4>in.<script>disable(document.forms[0].gap_$x)</script></td>\n";
	  }
	  
	  else
      {		  
	      echo "<td>&nbsp;<input type=hidden name=gap_$x ></td>\n";
      }
	  
	  echo "</tr>\n";
      
	  if ( $check==0 || $x == $check)
	  {
         break;
      }
  }
  if ( $check==0 )
  {
      echo "<tr><td colspan=2 bgcolor=pink>\n";
      echo "You have no configured units that are elegible for inclusion in a lineup.  This functionality is ";
      echo "only for use with custom or RanChef units that have been configured in the past ".$DAY_LIMIT." days\n";
      echo "</td></td>\n";
  }
  else
      echo "<tr><td colspan=4 align=center><input type=submit value=Continue>\n";

?>

Posted: Tue Mar 04, 2003 6:34 pm
by m3mn0n
If you do everything you can it's probubly just the server load and the processor speed. I hear there is php script execution enhancers at Zend.com, you should look into thoses.

Posted: Wed Mar 05, 2003 3:59 pm
by BDKR
Other than the what Oromain says, you could also check the setup of the database tables in particular. Are they using indexes? This can have a dramatic effect on the speed of the system. I'd check this out.

On the other hand, if your tables don't have that many records in them at this point, it may be something else.

Another thing you can do is time your srcript or portions of it. Use this.

Code: Select all

function getmicrotime()
  {
  list($usec, $sec) = explode(" ",microtime());
  return ((float)$usec + (float)$sec);
  }
Anyways, just a couple of little things.

1) post incrementing is slower than pre. Unless there is a reason otherwise, do

Code: Select all

++$var
instead of

Code: Select all

$var++;
2) echo is expensive. In some places, you could concat the multiple echo strings then echo it all out at once. I was suprised at how much faster this was. But be careful too as it will take more memory to do it this way.

In some of those cases, you could also just go into HTML mode. That's also a good deal faster and lacks the memory overhead.

3) Where does/did $start_am and $user_info come from?

Cheers,
BDKR

Posted: Wed Mar 05, 2003 4:53 pm
by hedge
It looks like you are running a query in a loop.... that is usually slow. Change your logic to do one DB query and then level-break logic to process the cursor.

Cleaning the Code

Posted: Wed Mar 05, 2003 6:40 pm
by rodrigocaldeira
Hi,

try to eliminate the '{' in the if and else if it have only one instruction to do;

for example:

replace
if ($var == 0)
{
echo "something";
}

to
if (var == 0)
echo "something";

It's easier to read the script;

Re: Cleaning the Code

Posted: Thu Mar 06, 2003 7:50 am
by BDKR
rodrigocaldeira wrote:Hi,

try to eliminate the '{' in the if and else if it have only one instruction to do;

for example:

replace
if ($var == 0)
{
echo "something";
}

to
if (var == 0)
echo "something";

It's easier to read the script;
Some people (like me :twisted: ) feel this makes it tougher to read. I think it's bad practice.

But seriously, what does this have to do with performance?

Cheers,
BDKR

Re: Cleaning the Code

Posted: Thu Mar 06, 2003 7:57 am
by twigletmac
BDKR wrote:Some people (like me :twisted: ) feel this makes it tougher to read. I think it's bad practice.
and me, I think it's a horrible way of doing things - with braces ({ and }) at least you know where things start and finish.

Totally OT of course, just wanted to get my 2p in too.

Mac

Posted: Thu Mar 06, 2003 8:12 am
by PHP-Editors.com
Me too. I have even read PHP books that recomment not using braces... but saying that - the same book doesn't use super globals either !!
I think it comes down to your learning methods, previous language experience and personal preference. Still, its never to late to change your style !!

Posted: Thu Mar 06, 2003 11:41 am
by pootergeist
hedge wrote:It looks like you are running a query in a loop.... that is usually slow. Change your logic to do one DB query and then level-break logic to process the cursor.
yup - 12 repetetive queries are rediculous (especially as you are not using the incremental $x variable to change the query in any manner)

just move the

$sql="select j.job_number
from job j,job_opts o
where j.modify >=".$start_am."
and (j.engineer="".$user_info['name']."" or j.engineer2="".$user_info['name']."")
and (o.type=0 or o.type=2 or o.type=4 or o.type=8 )
and o.utype < 3
and o.job = j.job_number order by j.job_number";
$qry=ifx_query($sql,$dbid);

entirely out of the for loop (before it works best) and you'll only be querying the database once