Why is php slow?

(*-*)浩
(*-*)浩Original
2019-09-16 15:47:202820browse

Why is PHP slow?

Why is php slow?

PHP is slow compared to C/C level languages. In fact, the original design of PHP language , it is not used to solve computationally intensive application scenarios. We can roughly understand that PHP sacrifices execution efficiency in order to improve development efficiency. (Recommended learning: PHP programming from entry to proficiency)

We know that a big feature of PHP is the weak type feature, that is to say, I can define a variable at will , and then assign it to various types of data at will. Taking an int integer number as an example, in C language:

int num = 200;//通常是4字节

However, if PHP defines the same variable, the actual corresponding storage structure is:

This structure will occupy much more memory than C variables. It is defined in PHP as follows:

$a = 200;//这变量将实际占用对比C变量很多倍的存储空间。

In fact, for PHP, no matter what type of data is stored, the above " The structure implementation of "Tong Kill". In order to be compatible with PHP programmers' variable type "intrusion", PHP has been friendly to developers, but cruel to the execution engine. The memory consumption of a single variable may not be obvious yet. Once PHP arrays are used, the complexity increases exponentially (the implementation of arrays is HashTable).

Then, when the Zend engine executes, these PHP codes are compiled into opcode (PHP’s intermediate bytecode, the format is somewhat similar to assembly), which is interpreted and executed line by line by the Zend engine.

Whether it is the connection operation of strings or the simple modification of arrays, it is almost the rhythm of "one word from a PHP programmer, and the Zend engine will break its legs". Therefore, for the same operation, PHP consumes more system resources such as CPU and memory than C. In addition, there are automatic memory recycling, variable type judgment, etc., which will increase the consumption of system resources.

For example, I used the quick sort function and the native sort function implemented in pure PHP to sort 10,000 integer numbers to make a time-consuming comparison. The results are as follows:

The native sort takes time 3.44 ms, while our own implemented PHP function sort is 68.79 ms. We found that there is a huge gap in execution efficiency between the two.

My test method is to calculate the time interval before and after the function is executed, not the time from startup to end of the entire PHP script. The PHP script startup and shutdown process itself involves a series of initialization and cleanup work, which also takes up a lot of time.

Normally, the ranking of PHP execution efficiency is:

The fastest are PHP language structures (isset, echo, etc.), part of the PHP language (they are basically not a function).

Then the faster ones are PHP’s native and extended functions. PHP extension, based on Zend API, functions implemented in C, and the execution efficiency is of the same order of magnitude as C/Java.

What is really slow is the code and functions we write ourselves through PHP. For example, if we use a relatively heavy framework implemented in pure PHP, because the framework itself has many modules, it will obviously drag down the execution efficiency at the language level and occupy more memory. (The domestic Yaf framework is implemented in an expanded manner, so the execution efficiency is much faster than that of a framework written in pure PHP)

Under normal circumstances, we do not recommend using PHP to implement complex logical calculation functions. Especially in scenarios where the web system traffic is relatively large. Therefore, PHP programmers should have a relatively broad understanding of PHP's various native functions and various extensions. In specific function implementation scenarios, seek more native solutions (native interfaces or extensions) instead of writing one by themselves. Stack of complex PHP code to implement this type of functionality.

If you have enough PHP extension development capabilities, rewriting this type of business function as a PHP extension will also greatly improve the execution efficiency of the code. This is a very good method and is also widely used in PHP optimization. However, the shortcomings of self-written PHP business development are also obvious:

Expansion development takes a long time, and modifications are complicated when requirements change. Poor writing may affect the stability of Web services. (For example, in Apache's worker mode, if it hangs in a multi-threaded scenario, it will affect other normal sub-threads in the same process. If it is a multi-threaded Web mode, the writing extension needs to support thread safety)

Expansion When upgrading the PHP version, additional compatibility work may be required.

The maintenance and takeover costs after personnel changes are also relatively high.

In fact, among front-line Internet companies, the more common solution is not to add PHP expansion, but to use C/C to independently write a service server, and then PHP communicates with the service server through socket to complete business processing. It does not couple PHP itself with the business.

However, most of the performance bottlenecks of Web services are in the time-consuming of network transmission and other service servers (such as MySQL, etc.). The time-consuming of PHP execution accounts for a very small proportion of the overall time-consuming, so from From a business perspective, the impact may not be obvious.

The above is the detailed content of Why is php slow?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn