PHP performance Optimization (1)

Source: Internet
Author: User

Objective

PHP itself is already a very fast language, but in addition to the speed of execution, PHP There are still many places that can be optimized.

In this article we will introduce some of the following:

1. why There are many factors in PHP optimization are not related to the code

2. in the process of improving PHP Performance, why do we need to know more about other aspects of knowledge

3. these subsystems cause bottlenecks and solutions

4. We also discuss how to adjust and optimize the PHP code to make it better performance

Achieve High performance

When we talk about high performance, we don't just mean How fast a PHP script is running, performance is a tradeoff between speed and scalability. Scripts that use fewer resources should be slower than another script that uses caching, but in a Web Server, you might run multiple copies of the same script at some point in time.

       in the following example, we assume that the scripta.phpis a very fast runner, andb.phpis a marathon runner, his speed is basically the same. When the load is lighter,a.phprun more thanb.phpfast, however, withWebthe load on the server is increasing,b.phpthe performance was only reduced by some, whilea.phpbut it's hopeless!

now let's take a real example to explain what happened. We're going to write aPHPscript, which from a250kfile, and generates aHTMLfiles. For ease of comparison, we have written two scripts that implement the same functionality: "hare.php" reads the file into memory at once and processes all data at once;tortoise.php" reads only one row from a file at a time and never retains more than a single line of information in memory. The result istortoise.phpbecause of the use of more system calls and obviously slow andhare.php.

       Scripthare.phprequires a0.04seconds ofCPUTime and 10M of Memory,tortoise.phprequires a0.05seconds ofCPUis time and 5M of memory. At this pointWebServer has 100M of physical memory, and the% of IdleCPU. In order to simplify the problem, we do not consider the case of memory fragmentation.

       when there isTenwhen a script is run at the same time,hare.phpwill take out all of the memory ( 10M xTen= 100M ),andtortoise.phpYou can have the rest 50M of free memory. If the first Onescript to run on the server,hare.phpwould have to use virtual memory, which could cause it to halve its speed, when all thehare.phpthe request will use the0.88seconds ofCPUtime, meanwhile,tortoise.phpwill still use0.06seconds ofCPUtime.

the faster PHP scripts in the following table use bold to differentiate:

Number of connections

1 a HTTP at the time of the connection CPU Time

10 http cpu time

One a HTTP at the time of the connection CPU Time

hare.php

0.04

0.40

0.88
( virtual memory used )

tortoise.php

0.06

0.06

0.66

The above example shows that providing good performance does not simply mean writing a fast PHP script. High-performance PHP also requires a good understanding of the underlying hardware, operating system, and commonly used supporting software like Web servers and database systems.







</

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.