Making PHP websites run faster --- how to optimize PHP

Source: Internet
Author: User
Tags server array
Php (as the mainstream development language) is a very rapid execution programming language, but it is still worth optimizing php (as the mainstream development language) itself than just optimizing code. This article will explain why optimizing php (as the mainstream development language) is better than optimizing SyntaxHighlighter. all ()
Php (as the mainstream development language) is a very rapid execution programming language, but it is still worth optimizing php (as the mainstream development language) itself than just optimizing code.
  
This article will explain why optimizing php (as the mainstream development language) is more appropriate than optimizing code based on some actual results, and why you need to understand how to locate and fix bottlenecks based on the performance of other subsystems on your server in php (as the mainstream development language. At the same time, we also talked about how to optimize your php code (as the mainstream development language) to make it faster to execute.
  
   High performance
When talking about good performance, we often don't just mean how fast your php (as a mainstream development language) 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,. php (as the mainstream development language) counts as a race runner as fast as possible, and B. php (as the mainstream development language) is a marathon that can run almost at the same speed forever. A. php (as the mainstream development language) can be fast enough, but when the traffic increases, B. the performance of php (as the mainstream development language) will be reduced by A little while. php (as the mainstream development language) will crash.
  
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 (as the mainstream development language) will read the entire file to the memory at a time, and then execute it in place step by step; and tortoise. php (as the mainstream development language) reads only one row of a file at a time and never exceeds the content capacity. As a result, Tortoise. php (as the mainstream development language) is much slower because multiple reads and writes require more system responses. "
Every execution of the program, hare. php (as the mainstream development language) requires 0.04 seconds of CPU execution time and 10 MB of memory, while tortoise. as the mainstream development language, 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 are executed concurrently, the memory overflow (10 × 10 = 100) occurs in hare. php (as the mainstream development language ). At the same time, tortoise. php (as the mainstream development language) will still have 50 MB of free memory available! Concurrent execution of 11 programs will enable hare. php (as the mainstream development language) is completely "broken" because it begins to need virtual memory-the execution speed may be lower than half of its normal speed; in addition, it takes 0.08 seconds for each individual program process to run the CPU. During this period, tortoise. php (as the mainstream development language) still runs in its conventional CPU execution time-0.06 seconds!
In the following table, php (as the mainstream development language) 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 (as the mainstream development language) | 0.04 | 0.04 | 0.88 (memory overflow) |
Bytes ------------------------------------------------------------------------------------------
| Tortoise. php (as the mainstream development language) | 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 (as the mainstream development language) programs. High-Performance php (as the mainstream development language) performance requires a good understanding of the underlying hardware knowledge, operating systems, software support, such as Web servers, databases, and so on.
  
   Bottleneck
The above two examples show the bottleneck of (performance) decline. With unlimited memory capacity, hare. php (as the mainstream development language) is always faster than tortoise. php (as the mainstream development language. However, it is too simple to think that memory is the bottleneck of the overall performance of php (as the mainstream development language)-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 every php (as the mainstream development language) page is 30 kB, only 33 pages are transferred per second, so your network bandwidth will be 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 (as the mainstream development language), 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.