| Wednesday, 18 June 2008 |
| 26 Tips to optimising your PHP pages |
Here are a selection of tips to help increase the speed of your PHP pages.- Using echo is faster than using print.
- Set the maxvalue for for-loops before the loop and not actually inside it.
- Unset your variables frees up memory, especially when you use large arrays. Its also good coding practice.
- When using includes and requires, use their full path, that way less time is spent on resolving the OS paths.
- If you need to find out the time when the script started executing, $_SERVER[?REQUEST_TIME?] is preferred to time()
- If a method can be static, declare it as such. This simple change will improve speed by a factor of 4.
- Try where possible to use strncasecmp, strpbrk and stripos instead of regex.
- str_replace is faster than preg_replace. strtr however is faster than str_replace by a factor of 4.
- Switch statements are far better than multi if / else if statements, it is also far more readable.
- Error suppression with @ is very slow, so only use when debugging or testing.
- If possible, turn on apache's mod_deflate, this will compress the output from the server.
- Remember to close database connections when you're done with them.
- Simple change, $row[?id?] is 7 times faster than $row[id]
- Error messages are expensive, try to only use the majority of them for testing and debugging.
- Do not use functions inside of for loops, such as for ($x=0; $x < count($array); $x) The function gets called each time, not great for speed!
- Incrementing a global variable is 2 times slow than a local variable.
- Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable.
- Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.
- Remember to remove any variables that are declared but not used, the parser does some checking which takes a little time.
- Surrounding your string by ' instead of " will make things interpret a little faster since php looks for variables inside "..." but not inside '...'.
If you need to include variables in this then just add them in with '...' . $var . '....'
- PHP scripts are recompiled every time they are called unless the scripts are cached. Try and install a PHP caching product, this typically increases performance by 30-100% by removing compile times.
- Cache as much as possible. Use memcached - memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load. OP code caches are useful so that your script does not have to be compiled on every request
- Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it's execution does not require function lookups and lowercase.
This means you have virtually no overhead on top of the actual code that determines the string's length.
- OOP is great, but only use it where it is required, sometimes code is much quicker when it is denormalised, i.e. not OOP.
- Try to profile your code using a profiler. This will show you which parts of your code consumes how much time.
The Xdebug debugger already contains a profiler. Profiling shows you the bottlenecks which you can then rectify.
- Try and use mod_gzip which is available as an Apache module compresses your data on the fly and can reduce the data to transfer up to 80%
|
|
|