1. Origin
About PHP, a lot of people's intuitive feeling is that PHP is a flexible scripting language, library class rich, easy to use, safe, very suitable for web development, but low performance. Is the performance of PHP really as bad as everyone feels? This article revolves around such a topic to discuss. From the source code, application scenarios, benchmark performance, comparative analysis of several aspects of the performance of PHP in-depth analysis, and through the real data to speak.
2. Analyze PHP performance from the principle
From the principle analysis of PHP performance, mainly from the following aspects: memory management, variables, functions, operating mechanism to carry out the analysis.
2.1 Memory Management
Like Nginx memory management, PHP is also based on the memory pool, and introduces the lifecycle concept of the memory pool. In the memory pool, PHP hosts all of the memory-related operations of PHP scripts and extensions. The management of large memory and small memory adopts different implementation methods and optimization, and can refer to the following documents: HTTPS://WIKI.PHP.NET/INTERNALS/ZEND_MM. During the lifecycle of memory allocation and recycling, PHP uses an initialization application + dynamic expansion + memory identity recovery mechanism, and mask the memory pool directly after each request is completed.
2.2 Variables
As is known to all, PHP is a weak variable type language, so inside PHP, all PHP variables correspond to a type zval, which is defined as follows:
Figure A PHP variable
In terms of variables, PHP does a lot of optimization work, such as reference counting and copy on writer mechanism. This ensures optimal memory usage and reduces the number of memory copies (refer to http://blog.xiuwz.com/2011/11/09/php-using-internal-zval/). In the array aspect, the PHP internal uses the efficient hashtable to realize.