Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Macroscopic Analysis of PHP performance

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is "Macro Analysis of PHP performance". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "macro analysis of PHP performance" bar!

At the macro level, that is, the performance analysis of PHP itself is divided into three aspects:

The performance of PHP as an interpretive language has its natural defects.

As a dynamically typed language, PHP also has room for performance improvement.

Language engine performance of the current mainstream PHP version itself

1. Performance analysis and improvement of PHP as an interpretive language

PHP, as a scripting language, is also an interpretive language, which is the reason for its natural performance limitation, because unlike compiled languages, which are compiled into binary code before running, interpretive languages face the input, parsing, compilation and execution of the original script at each run. The following is the execution process of PHP as an interpretive language.

As shown above, as you can see from the figure above, each run requires three processes of parsing, compiling, and running.

So where is the point of optimization? As you can imagine, as long as the code file is determined, the step from parsing to compilation is certain, because the file no longer changes and executes, depending on the input parameters. In the world of performance optimization, the supreme trick is to reduce operations while achieving the same results, which is known as caching. Caching is ubiquitous, and caching is a killer for performance optimization. As a result, OpCode caching appears, only * times need to be parsed and compiled, and in the later execution, directly from the script to Opcode, thus achieving performance speed. The execution process is shown in the following figure:

Compared with each parsing, compilation, and after reading the script, the efficiency of reading bytecode directly from the cache will be greatly improved. How much will the improvement be?

Let's do an experiment without Opcode cache. 20 concurrent requests with a total of 10000 requests not cached by opcode. The result is as follows:

Second, we open the Opcode cache on the server. To implement opcode caching, you only need to install the APC, Zend OPCache, and eAccelerator extensions, and even if you install more than one, only one of them is enabled. Note that after you modify the php.ini configuration, you need to reload the php-fpm configuration.

Here, APC and Zend OPCache are enabled to do experiments respectively. Enable the version of APC.

As you can see, the speed has been greatly improved. The original 110ms for each request processes 182 requests per second. After APC is enabled, 68ms processes 294 requests per second, increasing the speed by nearly 40%.

In the Zend Opcache-enabled version, you get roughly the same result as APC. 291 requests are processed per second, and each request takes 68.5ms.

As you can see from the above experiment, the test page used has more than 40ms time spent on syntax parsing and compilation. By caching these two operations, the speed of this process can be greatly increased.

In addition to what OpCode is and the bytecode after OpCode compilation, we can use a tool like bytekit or use the vld PHP extension to compile the PHP code. The following is the running result of the vld plug-in parsing code.

You can see that each line of code is compiled into the corresponding OpCode output.

2. Performance analysis and improvement of PHP as a dynamically typed language

The second is that PHP language is a dynamically typed language, dynamically typed language itself because it involves type inference in memory, for example, in PHP, two integers add up, we can get integer values, an integer and a string add, or even two strings add, all become integer addition. The string and any type of concatenation operation become a string.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report