BKJIA quick translation] The Google team shared their website acceleration tips last week, including some PHP performance skills. However, these PHP performance skills caused a storm in the PHP community, and the PHP team immediately wrote an open letter to these performance skills "catch insects ". For Google's suggestions, BKJIA has been sorted out over the past few days and published the article "acceleration, acceleration, and re-acceleration: a complete list of website Acceleration Techniques from Google". The second half of the fourth page focuses on PHP-related performance skills. You can familiarize yourself with this article before reading this article.
The following is a translation of letters from the PHP Group:
PHP team: suggestions in this articleAllAll are errors.
1. No reason. Try not to copy variables.
The Zend engine of PHP 4 and 5 uses a memory management system named "copy-on-write. That is to say, no matter how many times you assign the value of a variable to another variable, data will not be copied as long as you do not change the value. Example:
- $data = str_repeat("*", 512 * 1024); // synthesize 512K of data
- $memory_used_before = memory_get_usage();
- $more_data = $data;
- $memory_used_after = memory_get_usage();
- print "Before: {$memory_used_before}\nAfter: {$memory_used_after}\n";
PHP 5.3 with thread-safety and debugging functions:
Earlier: 853968
Later: 854236
PHP 5.2 Without thread-safety and debugging functions:
Earlier: 581912
Later: 581976
That is, there is a 268-byte difference in the debugging mode, which is commonly used in the normal mode) There is a 64-byte difference. This is a far cry from the BKJIA Editor's note: in Google's original description, copying a variable will "double the memory consumption.
It should be noted that the PHP Code does not allow you to echo or store the original content of the variables provided by the user without proper filtering.
2. Use single quotes for long strings
The benchmark tests for PHP 5.2 and 5.3 show that although the double quotation marks use interpolation) and the single quotation marks use concatenation), the speed of the two is completely consistent, and even the double quotation marks are often faster ). When using a common string without variables, double quotation marks are superior in performance.
3. Use echo instead of print
The running speed of these two methods depends on how your PHP is set on the host.
4. Do not use concatenation together with echo)
The opposite is true. The new engine processes multiple echo methods, resulting in faster use of concatenation in echo.
5. replace if/else with switch/case
Finally, this suggestion is completely nonsense. Deciding where to use the switch/case or if/else depends entirely on the encoding habits, and their running speed is basically the same except in certain situations.
In fact, most of these suggestions are correct in older PHP versions, PHP 3 and very old PHP 4. However, these are definitely incorrect in the next generation of PHP.
- Acceleration, acceleration, and re-acceleration: a complete list of website Acceleration Techniques from Google
- Google launches the Web Acceleration Program to improve TCP/IP and HTTP protocols
- Google research team's latest report: speed is the most important on the Internet!
- Google will launch the developer community to improve Web performance
- Google launches webpage acceleration tool for developers Page Speed