Blog

PHP Performance and Efficiency Research


PHP File After researching about PHP performance and efficiency optimization, I have these important things to focus on:
  1. Profiling the PHP code
  2. Upgrading to latest stable PHP
  3. Responsible Caching
  4. Avoid useless functions and variables
  5. Avoid multiple SQL queries (specially for remote databases)
  6. Understand PHP features like garbage collection
  7. Using functions and constructs properly

Profiling your PHP code to pinpoint bottlenecks

Before trying to optimize your PHP code, you have to know which part of your code is the slowest.  To find this out you have to profile your code.
profiling ("program profiling", "software profiling") is a form of dynamic program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or frequency and duration of function calls
By this you will know where your bottlenecks(i.e. components that slows down the whole system) are and start optimizing them.
A bottleneck is a phenomenon where the performance or capacity of an entire system is limited by a single or limited number of components or resources.
Bottlenecks could be a bad regular expression on a preg_match, a very long iteration (e.g. foreach, for, while), a bad SQL query on a mysql_query, a connection(e.g. cURL) to a non-responding server or even infinite loop. Usually this could be fixed by code optimization, but in some cases you really have to do some caching to relieve the load.  

Actions-go-up-iconUpgrading to latest stable PHP

The team of developers who maintain the PHP engine have made a number of significant performance improvements over the years. If your web server is still running an older version, such as PHP 3 or PHP 4, you may want to investigate upgrading before you try to optimize your code.
In my experience I only upgrade to have bugs fixed and have new features, as for performance, I don't notice much difference between versions. About every month there is a new minor version release found on PHP download page. Or you can also do an upgrade using the package manager(e.g. apt-get upgrade)  (configuration maybe needed)  

Responsible caching

Actions-document-save-as-icon

cache is a component that transparently stores data so that future requests for that data can be served faster
Not just caching, you have to do it responsibly. Finding a way to do this accurately and efficiently at the same time is sometimes profound but sometimes tricky. It depends on the situation. For example, Smarty, a popular templating engine have found an accurate way to compile the templates, which renews every time one of the template files involved is modified. There's a lot of things to cache according to your application, here are some examples
  1. static database tables - those tables that you do not update too often (e.g. categories)
  2. static pages
  3. static includes
As for the framework we are working on, we have developed a caching class to be included on the future releases.

Avoid useless functions and variables

Actions-trash-empty-iconCreating a function assuming that it'll be used again in the future often ends up not being used again at all. So instead of thinking for the future, think just for what you see in the present code and create such functions and variables as you see that it'll be really be re-used somewhere else in the script. On my team I have this rule that we will gonna use the function/variable at least twice on the code, they shouldn't be function/variable at all.   Avoid multiple SQL queries (specially for remote databases) Apps-kexi-iconFor remote database servers, avoid multiple SQL queries (e.g. for insert queries) as much as possible. This is to reduce the time used in traveling from server to server, specially if you're using free shared hosting which usually has separate server for database.  

Understand PHP features like garbage collection

Apps-kthesaurus-iconEven though it sounds boring, knowledge about such feature as garbage collection (gc) is also useful. Other features worth considering include: persistent database connection, connection handling, and a lot others like class autoloading and variable references. http://php.net/manual/en/features.php  

Using functions and constructs properly

Categories-preferences-system-iconProper knowledge of which functions or construct is optimized for different uses can be useful for faster php scripts. For example, for finding out if the variable is an email,  you can use filter_var() instead of preg_match(). Knowing the right function to use is usually learned by experience, and frequent research. For me it actually took me a long while to know that there is a built in email format checker. You can also check the benchmarks already done by other people like the following: http://maettig.com/code/php/php-performance-benchmarks.php http://www.phpbench.com/  

Conclusion

Mimetypes-text-x-katefilelist-iconIt is important that we strive for better scripting habits, but this is just one of the things we should consider. The requirements of performance and speed of our script depends from application to application and we shouldn't always put performance as top priority. We should always balance things up when it comes to developing.             Sources Actions-document-open-remote-icon https://developers.google.com/speed/articles/optimizing-php http://php.net/manual/en/features.gc.performance-considerations.php http://php.net/releases/index.php http://en.wikipedia.org/wiki/Profiling_(computer_programming) http://en.wikipedia.org/wiki/Bottleneck http://en.wikipedia.org/wiki/Cache_%28computing%29      

Comments (0)


Add a Comment





Allowed tags: <b><i><br>Add a new comment:


A Few Accomplishments

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer non. Adipiscing cubilia elementum integer. Integer eu ante ornare amet commetus.

Possibly broke spacetime

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer adipiscing ornare amet.

Terraformed a small moon

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer adipiscing ornare amet.

Snapped dark matter in the wild

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer adipiscing ornare amet.

Contact Me

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer non. Adipiscing cubilia elementum integer. Integer eu ante ornare amet commetus.