Make PHP websites run faster---How to optimize PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:33:43
Original
874 people have browsed it


 php(as the current mainstream development language) is a programming language that executes very quickly, but it is still worth optimizing php(as the current mainstream development language) compared to just optimizing the code. Mainstream development language) itself.
 
In this article, we will explain based on some practical results why optimizing php (as the current mainstream development language) itself is more appropriate than optimizing code, and why we need to understand how to optimize php (As the current mainstream development language) Find bottlenecks in the performance of other related subsystems on your server and fix them. At the same time, we also mentioned how to optimize your php(as the current mainstream development language)code to make them execute faster.
 
 Get high performance
When we talk about good performance, we often don’t just refer to your php(as the current mainstream development language)code How fast it executes. Performance is a set of tradeoffs between quantifiable measurements and speed. Code that simply relies on using fewer resources may execute more slowly than code that runs in the cache, and the same set of code (that executes in the cache) can be executed concurrently on a Web server at the same time.
In the example below, A.php (as the current mainstream development language) is regarded as a runner who runs as fast as possible, and B.php (as The current mainstream development language) is a marathon runner that can run almost forever at the same slow speed. Under light load, A.php(as the current mainstream development language) can be sufficiently fast, but when the traffic increases, B.php(as the current mainstream development language) will only drop a little and A.php (as the current mainstream development language) will collapse.  

Make PHP websites run faster---How to optimize PHP_PHP tutorial
Let us verify the more profound and essential meaning of this statement through a fact. Suppose we need to read a 250K file and generate an HTML summary of the file. We wrote two scripts to do the same thing: hare.php
(as the current mainstream development language) will read the entire file into memory at once and then execute it in one step; and tortoise. php(as the current mainstream development language)only reads one line of the file at a time and never exceeds the content capacity. As a result, Tortoise.php(as the current mainstream development language) is much slower because multiple reads and writes require more system responses. " Every time the program is executed, hare.php
(as the current mainstream development language) requires 0.04 seconds of CPU execution time and 10Mb of memory, while tortoise.php (as the current mainstream development language) Mainstream development language) requires 0.06 seconds of CPU execution time and 5Mb of memory. The server has a total of 100Mb of actual memory capacity and its CPU is 99% idle. We also assume that executing such a simple event does not cause memory fragmentation. When 10 programs are executed concurrently, hare.php(as the current mainstream development language)
will have a memory overflow (10 × 10 = 100). At the same time, tortoise.php( As the current mainstream development language) there will still be 50Mb of free memory available! The concurrent execution of 11 programs will completely "collapse" hare.php (as the current mainstream development language) because of it Started using virtual memory - potentially slowing execution to less than half its normal speed; and each individual program process now requires 0.08 seconds of CPU execution time.During this period, tortoise.php (as the current mainstream development language) is still running at its regular CPU execution time - 0.06 seconds!
In the following table, the faster-executing php (as the current mainstream development language) scripts are distinguished in bold:
----------- -------------------------------------------------- --------------------------
| Number of connections | CPU execution time required for each HTTP request (seconds) | CPU execution time required for each execution of 10 HTTP requests (seconds) | CPU execution time required for each execution of 11 HTTP requests (seconds) |
 ---------------- -------------------------------------------------- --------------------------
| hare.php (as the current mainstream development language) | 0.04 | 0.04 | 0.88 (memory overflow) |
 ---------------------------------------- --------------------------------------------------
| tortoise.php (as the current mainstream development language) | 0.06 | 0.60 | 0.66 |
---------------- -------------------------------------------------- -----------------------
As you can see in the example above, getting better performance is no longer just about writing faster and executing PHP (as the current mainstream development language) program. High-performance PHP (as the current mainstream development language) performance requires a good knowledge and understanding of underlying hardware knowledge, operating systems, and software support such as Web servers, databases, etc.
 
 Bottleneck
  The above two examples let us see the bottleneck of (performance) degradation. When having infinitely large memory, hare.php(as the current mainstream development language) is indeed faster than tortoise.php(as the current mainstream development language) . However, it is too simple to think that memory is the bottleneck of overall performance of php(as the current mainstream development language) - in fact it is much more than that:
 
 (a) Network
 
Your network may be the biggest bottleneck. If you have 10M bandwidth - at best you can only get a transfer speed of 1M/sec. If we assume that each php (as the current mainstream development language) page is 30k, then only transferring 33 pages per second will saturate your network bandwidth. More factors that lead to bottlenecks include frequent access to low-speed DNS, or network devices that only have limited access to storage.
 
 (b) CPU
 
 If you monitor your CPU load, sending a purely static HTML page will not increase the CPU load - as we mentioned above, at this time The bottleneck is the network. Of course, for complex dynamic pages generated by php(as the current mainstream development language), your CPU speed will naturally become one of the limiting factors. Having a server with multiple CPUs or a server array will lessen the impact of the CPU.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508602.htmlTechArticlephp (as the current mainstream development language) is a programming language that executes very quickly, but compared to It is still worth optimizing php just to optimize the code (as the current mainstream development language...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template