Image

Imagecmorrigu wrote in Imagephp 😊impressed

mixed php/html and generation time (holy @#$%)

Right, so I learned to code mixed html/php the sloppy way, switching back and forth whenever the mood struck. Lately, I've been realizing what a pain it is to figure out what's where when something needs to change.... so I switched to just putting all my output into a variable, then outputting it in a pesudo-template at the end of the script.



I've started working with larger datasets, so I added a timer function to display the time it took to generate each page in the footer.... I ran my old page a few times, and saw consisted 4+ second generation times. I changed it to the pseudo-template, and I'm getting consistent .2 second times.



I also read of a difference in using "stuff $php_variable stuff", which is slower than "stuff " . $php_variable . " stuff", so I changed that where appropriate as well.




Here's the old code:
while ($myrow = mysql_fetch_array($result)) {
  if (mysql_errno() != 0)	{ 
    echo "Error " . mysql_errno() ."\n <br>"; 
  } else { // end if (mysql_errno() != 0)
    ?>
      <tr>
       <td><?php echo $myrow["id"] ?> 
       <td><?php echo $myrow["title"] ?> 
       <td><?php echo $myrow["artist"] ?> 
       <td><?php echo $myrow["variant"] ?> 
       <td><?php echo $myrow["bpm"] ?> 
       <td><?php echo $myrow["genre"] ?> 
       <td><?php echo $myrow["stuff"] ?> 
       </tr>
     <?php
  } // end else
} // end while ($myrow = mysql_fetch_array($result))


Here's the new code:
while ($myrow = mysql_fetch_array($result)) {
  if (mysql_errno() != 0)	{ 
    echo "Error " . mysql_errno() ."\n <br>"; 
  } else { // end if (mysql_errno() != 0)
      $out .= "<tr>\n";
      $out .= " <td>" . $myrow["id"] . "\n"; 
      $out .= " <td>" . $myrow["title"] . "\n";
      $out .= " <td>" . $myrow["artist"] . "\n";
      $out .= " <td>" . $myrow["bpm"] . "\n";
      $out .= " <td>" . $myrow["genre"] . "\n";
      $out .= " <td>" . $myrow["stuff"] . "\n"; 
      $out .= " </tr>\n";
  } // end else
} // end while ($myrow = mysql_fetch_array($result))



This was for about 425 rows, I believe.



I had no idea it made THAT much of a difference... Guess I'll be using that on everything now... If not switching over to SMARTY or some other true templating system.