"Do not spoil what you have by desiring what you have not; but remember that what you now have was once among the things you only hoped for." -Epicurus
Optimize Your Code

Introduction

For a web site like mines where there is not a high number of traffic, optimization is not required. However, for big web sites like those that I have worked on such as BookingBuddy and Smarter Travel, optimization is a must. Optimizing your code will save your visitors a few milliseconds of the time they would otherwise wait and it saves your server unnecessary processing.

Things to Avoid

Many pre-defined functions are there for you to make your life easier. However, that does not mean you should always use them unless it is really necessary. These are some of the following things you should avoid using to speed up the execution of your code.

Avoid using magic functions should as __get, __set, and __autoload.

Avoid using require_once if you can because it is very expensive. I know it is hard to get rid of using require_once and I probably would not be able to avoid using it myself. However, do not use require_once if you are only using it to include a small file. If you do plan on using it, then make sure you use the full path to the file instead of using the relative path, even if you set the include path.

Always set the max value for a loop before and outside of the loop instead of within it. That means you should never use the following:

for($i = 0; $i < count($array); ++$i) {
    ...
}

If you use the previous code, count($array) will be called during each loop iteration. Consider setting the result of count($array) to a variable and use that in the loop instead:

$length = count($array);
for($i = 0; $i < $length; ++$i) {
    ...
}

Avoid using error suppression with @ as it is very slow. Considering turning the output of error messages off and logging your errors.

If something is a string, have quotes around them. It is much faster to use $data['id'] then to use $data[id].

Do not increment a global variable or an object property. Instead, copy it to a local variable, increment it and set it back if there is going to be a good amount of increments. By virtue, do not declare a global variable and not use it.

Better Alternatives

There are many ways to write the same application. Some are easier for the eye to read and others make the application run faster. Use the following alternatives to improve your code in terms of speed:

When using strings, use single quotes instead of double quotes. Using double quotes signal to PHP that there is a chance a variable could be part of the string which leads to unecessary checking.

If you are using an associative array, it is better to use the following code:

$keys = array_keys($array);
$length = count($array);
for($i = 0; $i < $length; ++$i) {
    ...
    $array[$keys[$i]]
    ...
}

It is much faster than using the foreach loop or the while loop.

When defining a method for an object, if the method can be a static method, declare it as such.

If you want to output something, use echo instead of print. Also, if you need to output the concatenation of multiple strings, use echo as a function call using the comma instead of the period for even faster performance.

Although using regex and its family of functions makes searching much easier, try using strncasecmp, strpbrk, and stripos if possible.

Use strtr to replace strings if possible. Otherwise, use str_replace and at a last resort, use preg_replace.

For those using MySQL as their database and you have PHP 5 or greater, consider using the new mysqli_ functions instead of the old mysql_ functions.

If you are storing the IP address, consider using the functions ip2long and long2ip instead of just storing it as a string.

Instead of using strlen to check to see if a string has the required length, use isset($string[length]) instead. So if you want to output an error message if the given string is less than 5 characters, use the following:

if(!isset($string[5])) {
    ... error message ...
}

If possible, use the pre-increment functionality instead of the post-increment functionality. ++$i is faster than $i++.

Other Tricks

Some other tricks that should improve the performance of your application includes:

Remember to unset your variables to free up memory when necessary. This is especially the case if you have a large array and do not intend to use it anymore.

If you are validating an e-mail address, considering checking the domain name first by using checkdnsrr.

You can return multiple values from a function by using the by reference functionality of PHP. Just prepend & before the variable in the function declaration. It is faster than returning the different values by storing them in an array.

User Comments

  • Posted By
  • Posted On
  •  
  • Comment

Comment Submission

Found something wrong with the information or you just want to speak your mind? Send us a comment.

  • Nickname:
  • Comment:
  • Captcha:
  • Captcha
This page was last modified on: Jun 09, 2008.