A brief highlight of this article: http://phplens.com/lens/php-book/optimizing-debugging-php.php
1. The more you understand the software you are using (Apache, PHP, IIS, your database) and the deeper your knowledge of the operating system, networking and server hardware, the better you can perform global optimizations on your code and your system.
2. For PHP scripts, the most expensive bottleneck is normally the CPU. Twin CPUs are probably more useful than two Gigabytes of RAM.
3. Compile PHP with the "configure –-enable-inline-optimization" option to generate the fastest possible PHP executable.
4. Tune your database and index the fields that are commonly used in your SQL WHERE criteria. ADOdb, the very popular database abstraction library, provides a SQL tuning mode, where you can view your invalid, expensive and suspicious SQL, their execution plans and in which PHP script the SQL was executed.
5. Use HTML caching if you have data that rarely changes. Even if the data changes every minute, caching can help provided the data is synchronized with the cache. Depending on your code complexity, it can improve your performance by a factor of 10.
6. Optimize your loops first. Move loop invariants outside the loop.
7. Use the array and string functions where possible. They are faster than writing equivalent code in PHP.
8. Pass objects and arrays using references in functions. Return objects and arrays as references where possible also. If this is a short script, and code maintenance is not an issue, you can consider using global variables to hold the objects or arrays.
9. For searching for substrings, the fastest code is using strpos(), followed by preg_match() and lastly ereg(). Similarly, str_replace() is faster than preg_replace(), which is faster than ereg_replace().
10. For processing XML, parsing with regular expressions is significantly faster than using DOM or SAX.
11. Unset() variables that are not used anymore to reduce memory usage. This is mostly useful for resources and large arrays.
12. For classes with deep hierarchies, functions defined in derived classes (child classes) are invoked faster than those defined in base class (parent class). Consider replicating the most frequently used code in the base class in the derived classes too.
13. Consider writing your code as a PHP extension or a Java class or a COM object if your need that extra bit of speed. Be careful of the overhead of marshalling data between COM and Java.
back to dev:Main Page

![[Main Page]](/modules/mediawiki/images/mediawiki.png)





