Tips Centre Sunday, May 28, 2017

Home Home | About Us About Us | Contact Us Contact Us

vrPakistanis Technologies - Web Development, Logo Designing, Web Designing

Home > PHP

Benchmark And Optimize PHP Script Speed

I found out the hard way why speed optimization in PHP is very important. Part of my work is developing a very complex digital learning system and this product has had it's first run a while ago. During the time the users in the system had grown from more than 3000 and more than 8000 usergroups. When this happened we found out the system didn't handle large numbers of users very well.

Through this, i learned that it is very important to start optimizing your code from the beginning of the project and apply a few best practices to speed up a script. A script that takes 0,01 second doesn't seem very long, but if you multiply this by 8000, it takes 80 seconds (!!!) to render a page...


In my experience it is the hardest part to find which part of a existing script is slowing the page down. In my case i thought it was somewhere at the end of the page, but later it turned out to be in the beginning of the page, when initializing the classes. Looking in the wrong place can cost you a lot of time!

One option is to comment everything in a page, and uncomment all the lines one by one. When the page slows down, you found the line where the slow part is.

But sometimes this isn't enough (not in my case) and you want to know how long a particular part of the script takes to execute. The second option is to divide the script in parts (not literally) and time the parts. If that isn't enough you can place a timer "around" every function or line, to get exact details on which part of the page takes how much time to execute.

I use this script to do this:


function getTime() {
    $timer = explode( ' ', microtime() );
    $timer = $timer[1] + $timer[0];
    return $timer;

And in a sample of code i use it this way:


$start = getTime();
    $aUsers = $wms->getUsers( 0, $showType, true, ($_REQUEST['page']*$perPage), $perPage );
$end = getTime();
echo  '<strong>getUsers/getUsersByAbc</strong>: '.round($end - $start,4).' seconden<br />';

In line 1 the starting time is stored in $start. In line 2 the funciton is executed and in line 3 the ending time is stored in $end. At last in line 4 a string is displayed with the time it took to execute the function (rounded at 4 decimals).


Now you found where the slowness is, you can apply these best practices:

  • Use static methods when possible, this is 4 times faster
  • Echo is faster as print
  • Use , instead of . to concatinate a string
  • Set the max value in a for-loop before the loop, not in the if statement.
    $max = count($array);
    for ($i=0;$i<$max;$i++) {
    echo $i;
  • Unset vars to clear memory (especially when using arrays)
  • Use full paths in includes and requires, so the server doesn't have to resolve the paths for you
  • Use strncasecmp, strpbrk and stripos in stead of regex
  • It's better to use a select statement then multiple if statements with multiple else statements
  • Suppress errors with an @ is very slow
  • Close database connections when you don't need them anymore
  • $row['id'] is 7 times faster as $row[id]
  • Incrementing a global var is 2 times slower as a local var
  • Wrap your string in single quotes (') instead of double quotes (") is faster because PHP searches for vars in "..." and not in '..'
  • A PHP script is 2 to 20 times slower as a HTML page. Use HTML pages when possible.
  • PHP scripts are compiled everytime you call them. Use PHP caching software to gain a speed optimization of 25% to 100%.
  • $i++ is slower as ++$i
  • Not everything has to be OOP. Most of the time OOP is just overhead, so use it wisely!
  • Use as much of the default functions of PHP, you don't have te reinvent the wheel!

I found this list with best practices on the internet and printed it out once, but i can't find the source of it.

This article has been taken from

Tips Centre 2008-2012. All rights reserved. Developed by vrPakistanis Technologies.