Press "Enter" to skip to content

PHP Performance Optimization

In April, I did a technical talk on PHP performance optimization.
Today I've uploaded the PPT from that talk to slideShare.
While I'm at it, let me share a few of my views on PHP performance optimization.
1. The purpose of optimization advice is not "optimization" itself, but to nip problems in the bud.
The optimization tips I listed in the PPT, written during coding, are not meant to be used as a set of criteria you apply to your code once a performance problem shows up. Rather, I hope that when you're first writing code, you already have a mental awareness of what style will turn out better.
2. Optimization advice is not a rule
When I shared the PPT with my colleagues at work, some questioned it: "Are we not even allowed to use magic methods?"
Optimization advice is advice — it's about stopping people from abusing them, from using them recklessly. If while writing code you can be aware of what's slow and what's fast, and thereby avoid some unnecessary calls to magic methods, then the optimization advice has achieved exactly what it sets out to do.
For example, in Yaf, I defined a magic method __set for the view engine, so some friends use it like this:

$this->getView()->user =  "laruence";
$this->getView()->age  = 28;
$this->_view->desc      = "PHP engineer";

In C this would be fine, but in PHP every set is a function call. So after seeing the performance optimization advice, you can realize that this can actually be replaced entirely by:

$this->getView()->assign(array(
      "user" => "laruence",
      "age"  => 28,
      "desc" => "PHP engineer",
));

And that's exactly the effect the optimization advice hopes to achieve.
3. Optimization advice is not universal — it's language-specific
Many PHPers used to write C, or maybe Java. A language is just a tool, but to use it well you should understand the tool's characteristics and craft different optimization strategies for different tools.
For example, C has a very powerful compiler "optimizer" that can do a lot of optimization for you, whereas PHP is an interpreted scripting language — it merely executes your code faithfully and does no optimization at all. So the style of code you write directly affects the final execution style.
Also, in C, symbols become addresses (for the most part) in the executor, whereas in PHP every symbol has to go through a lookup (Hash Lookup) before it can be used. Because of that, I've also seen code like the following:

for ($i=0; i<10;i++) {
   $arr[1][2][3][4][5] = $i;
}

Every loop iteration brings 6 Hash Lookups......
4. Get to the bottom of things
PHP is a very high-level language; using it lets you complete some features conveniently and concisely.
But dig to the bottom of it, and it's ultimately executed through C code, then assembly, then machine code. A PHP statement that looks simple may in the end take dozens or even hundreds of lines of C code to carry out.
Only by understanding this process can we truly make optimizations at the high-level language layer that have a positive impact on the underlying layers.

Finally, if slideshare is blocked for you, click here to download and watch: PHP Performance PPT

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.