0

I have a PHP page that loads and orders about 130 results from a mysql query into a HTML table. PHPmyadmin does this in 0.003 seconds, but my page takes 0.5 seconds. Somewhere the php code is slow, but I suck at debugging it.

One thing I did to speed up performance is that I found something like

if($var == '123'){ //action }

while $var did not exist. By changing it to

if(isset($var)){
  if($var =='123'){
    //action
  }
}

the script became much faster when $var was not around.

My question: what is the best way to find out the slowdown in my script? Are there more things like this?

Mbrouwer88
  • 2,182
  • 4
  • 25
  • 38

3 Answers3

1

How do you return the results? echoing within a loop may cause some performance issues, it's a very expensive instruction. If you do, change your implementation to something like this:

$resultOutput = '';

foreach($result as $var)
{
    // to seize AbsoluteZERO's idea
    switch ($var)
    {
        case "456":
        //do something
        $resultOutput .= '\n456 found';
        break;

        case "123":
        //do something
        $resultOutput .= '\n123 found';
        break;

        default:
        //give them an error or something
    }
}

echo $resultOutput;
actc
  • 672
  • 1
  • 9
  • 23
0

You can use an && (and) logical operator instead of the nested if:

if(!empty($var)&&($var=='123')){
    //action
}

If you're checking for multiple values with loose comparisons ==, instead of a nested if you can use a switch():

if(!empty($var)){
  switch ($var){

   case "456":
   //do something      
   break;

   case "123":
   //do something
   break;

   default:
   //give them an error or something

  }
}
AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35
0

This shouldn't have made much difference to the performance unless there is somethng very strange about your code / config you've not told us about. Try running your code through a profiler. (I use xdebug + kcachegrind - but you didn't say what OS you are using).

How do you get your page load times? PMA does't normally show a full result set. 0.003 seconds seems a bit optimistic. Not sure I trust your analysis. But certainly 0.5 seconds is very slow.

symcbean
  • 47,736
  • 6
  • 59
  • 94