Code Optimization

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Code Optimization

Post 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";

?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post 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.
rodrigocaldeira
Forum Commoner
Posts: 27
Joined: Wed Mar 05, 2003 6:40 pm
Location: Brazil
Contact:

Cleaning the Code

Post 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;
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: Cleaning the Code

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: Cleaning the Code

Post 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
PHP-Editors.com
Forum Newbie
Posts: 4
Joined: Mon Feb 03, 2003 9:23 am

Post 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 !!
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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
Post Reply