BKJIA Quick Translation】The Google team shared its website acceleration tips last week, including some PHP performance tips. However, these PHP performance tips caused an uproar in the PHP community, and the PHP team immediately wrote an open letter to "catch bugs" about these performance tips. Regarding Google's suggestions, BKJIA has sorted them out in the past few days and published an article "Accelerate, Accelerate, Accelerate Again: A Complete Collection of Website Acceleration Techniques from Google". The second half of the fourth page is dedicated to PHP-related performance skills. You can familiarize yourself with it before reading this article.
The following is a translation of the letter from the PHP team:
PHP Team: all of the advice given in this article is wrong.
1. Try not to copy variables without any reason
PHP 4 and 5 core Zend engine uses a memory management system called "copy-on-write". In other words, no matter how many times you assign the value of one variable to another variable, as long as you do not change the value, the data will not be copied. Example:
<ol class="dp-c"> <li class="alt"><span><span class="vars"><font color="#dd0000">$data</font></span><span> = </span><span class="func">str_repeat</span><span>(</span><span class="string"><font color="#0000ff">"*"</font></span><span>, 512 * 1024); </span><span class="comment"><font color="#008200">// synthesize 512K of data </font></span><span> </span></span></li> <li class=""> <span></span><span class="vars"><font color="#dd0000">$memory_used_before</font></span><span> = memory_get_usage(); </span> </li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$more_data</font></span><span> = </span><span class="vars"><font color="#dd0000">$data</font></span><span>; </span> </li> <li class=""> <span></span><span class="vars"><font color="#dd0000">$memory_used_after</font></span><span> = memory_get_usage(); </span> </li> <li class="alt"> <span>print </span><span class="string"><font color="#0000ff">"Before: {$memory_used_before}\nAfter: {$memory_used_after}\n"</font></span><span>; </span> </li> </ol>
Under PHP 5.3 with thread-safety and debugging functions:
Before: 853968
After: 854236
Under PHP 5.2 without thread-safety and debugging functions:
Before: 581912
After: 581976
That is, there is a 268-byte difference in debug mode, and a 64-byte difference in normal mode). This is far from the BKJIA editor's note stated in Google's article: In the description of Google's original article, copying variables will "result in double memory consumption").
It should be noted that it is strictly prohibited in PHP code to echo or store the original content of variables provided by the user without proper filtering.
2. Use single quotes for long strings
Benchmark tests for PHP 5.2 and 5.3 show that although double quotes use interpolation and single quotes use concatenation, the speed of the two is exactly the same, and even double quotes are often faster). When using ordinary strings that do not contain variables, the performance of using double quotes is significantly better.
3. Use echo instead of print
The running speed of these two methods depends on how your PHP is set up on the host.
4. Do not use concatenation chain with echo)
The opposite is true. The new engine's method of handling multiple echoes makes it actually faster to use concatenation in echo.
5. Use switch/case instead of if/else
Finally, this advice is complete nonsense. Deciding where to use switch/case or if/else depends entirely on coding habits, and they all run about the same speed except in certain circumstances.
In fact, most of these suggestions are correct under older PHP versions (PHP 3 and very old PHP 4 versions), but under the new generation of PHP, these are definitely wrong.