PHP is a programming language that runs very quickly, but it is worth optimizing PHP itself than just optimizing code. This article will explain why optimizing PHP is more appropriate than optimizing code based on some actual results, and why you need to understand how to locate the bottleneck and fix it based on the performance of other subsystems related to PHP on your server. At the same time, we also talked about how to optimize your PHP code to make it faster. High performance when PHP is a very fast-running programming language, it is still worth optimizing PHP itself than just optimizing code.
This article will explain why optimizing PHP is more appropriate than optimizing code based on some actual results, and why you need to understand how to locate the bottleneck and fix it based on the performance of other subsystems related to PHP on your server. At the same time, we also talked about how to optimize your PHP code to make it faster.
High performance
When talking about good performance, it often doesn't just mean how fast your PHP code is executed. Performance is a balance between quantifiable evaluation and speed. Code execution with less resources may be slower than the code with rows in the cache, and the same group (executed in the cache) code can be concurrently executed on a Web server.
In the following example, A. php is regarded as A runners who can run as fast as possible, while B. php is A marathon who can run almost at the same slow speed forever. Under light loads, A. php can be sufficiently fast, but when the traffic increases, B. php's performance will only decrease A little while A. php will collapse.
Let's use a fact to verify the more profound essential meaning of this statement. Suppose we need to read a k file and generate an HTML summary of the file. We wrote two scripts to do the same thing: hare. php will read the entire file to the memory at a time, and then execute it in place step by step; and tortoise. php reads only one row of a file at a time and never exceeds the content capacity. As a result, Tortoise. php is much slower because multiple reads and writes require more system responses. "
Each time the program runs, hare. php requires 0.04 seconds of CPU execution time and 10 MB of memory, while tortoise. php requires 0.06 seconds of CPU execution time and 5 MB of memory. The server has a total memory capacity of 99% MB and of its CPU is idle. We also assume that executing such a simple event does not generate memory fragments.
When 10 programs run concurrently, the memory overflow (10 × 10 = 100) occurs in hare. php ). At the same time, tortoise. php will still have 50 MB of free memory available! Concurrent execution of 11 programs will enable hare. php is completely "broken" because it starts to use virtual memory-the execution speed may be lower than half of its normal speed, and now each individual program process needs 0.08 seconds of CPU execution time. During this period, tortoise. php still runs at its regular CPU execution time-0.06 seconds!
In the following table, PHP scripts that run faster are separated by bold:
Bytes ------------------------------------------------------------------------------------------
| Connections | CPU execution time per HTTP request (in seconds) | CPU execution time per 10 HTTP requests (in seconds) | CPU execution time per 11 HTTP requests (in seconds) |
Bytes ------------------------------------------------------------------------------------------
| Hare. php | 0.04 | 0.04 | 0.88 (memory overflow) |
Bytes ------------------------------------------------------------------------------------------
| Tortoise. php | 0.06 | 0.60 | 0.66 |
Bytes ------------------------------------------------------------------------------------------
As you can see in the above example, to achieve better performance is not just to write and execute faster PHP programs. High-Performance PHP requires a good understanding and understanding of underlying hardware knowledge, operating systems, software support, such as Web servers and databases.
Bottleneck
The above two examples show the bottleneck of (performance) decline. Hare. php is always faster than tortoise. php when it has an infinite amount of memory. However, it seems too simple to think that the memory is the bottleneck of PHP's overall performance-actually far more than this:
(A) network
Your network may be the biggest bottleneck. If you have 10 Mbps of bandwidth, you can only get 1 Mbps of transmission speed. If each PHP page is 30 k, only 33 pages are transferred per second, which will make your network bandwidth saturated. More factors that cause bottlenecks include frequent access to low-speed DNS, or limited storage for network devices.
(B) CPU
If you monitor your CPU load, sending a pure static HTML page will not increase the CPU load-as we mentioned above, the bottleneck lies in the network. Of course, for complex dynamic pages generated by PHP, your CPU speed will naturally become one of the constraints. A server or a server array with multiple CPUs can reduce the impact on the CPU.