) Accelerate PHP ECHO

Source: Internet
Author: User
(Echowww.laruence.com201102131870.html of PHP for acceleration) you may have noticed that when ECHO is used in PHP to output a large string, the execution time will be significantly long, so some friends may think that PHP's ECHO performance is very poor. I have explained the cause in my previous article and hope to correct the misunderstanding of "poor ECHO performance of PHP. however, in previous articles, (to) accelerate PHP ECHO
Http://www.laruence.com/2011/02/13/1870.html

You may have noticed that when using ECHO in PHP to output a large string, the execution time is obviously long, and some may think that PHP's ECHO performance is very poor.

I have explained the cause in my previous article and hope to correct the misunderstanding of "poor ECHO performance of PHP.

However, the previous article only gave the reason and did not introduce how to avoid this problem. Today, a product line (Apache with PHP) in the company found a problem, some users initiate a large number of download requests in a short period of time, resulting in a sharp increase in the number of http connections and database connections,

The reason for the sharp increase in the number of database connections is that the database connection is in single-column mode and will not be released until the request processing is complete. in this way, a problem occurs. if the request processing time is too long, a large number of database connections may exist.

This user's network speed is very slow, which means that ECHO's "performance" is very poor ~, The download takes a long time ~. As shown in:


ECHO execution

This raises the question that I want to talk about today. how can we make ECHO faster and end the PHP request processing process as soon as possible...

We know that the reason why ECHO is slow is to wait for "write data" to return a successful response. a simple method is to open the output cache,

Edit php. ini

Output_buffering = 4096 // byte of course, you can also call ob_start () in the script ():

Ob_start (); echo $ huge_string; // other logic. ob_end_flush (); here, there is a place to note, ob_start will open up a buffer of 4096 size, so if the huge_string is greater than 4096, it will not accelerate.

Now, our ECHO will "immediately" run successfully and return. because the data is temporarily written to our output cache. if the buffer size is large enough, the content will be sent to the client (strictly speaking, it is sent to WebServer) at the end of the script ).

However, this does not solve the problem we encountered today, because the data needs to be sent to the client by PHP (the Output buffer of WebServer is not considered at this time) at the end ), this process will not end, the request will not be closed, and PHP will not execute the DB destructor ~

Now that you are dreaming, you can use Apache's output cache to change it to the following execution process:


Acceleration ECHO

Assume that PHP needs to output 100 kB of data, so the Apache output cache must be greater than kb. otherwise, when the Apache output cache is full, it will actually be sent to the client, and in this process, the ECHO executed at that time will block the waiting.

So how can I modify the Apache output cache? We can use the SendBufferSize configuration command in the apache configuration file:

SendBufferSize 4096 // note that byt specific SendBufferSize description, see the http://httpd.apache.org/docs/2.0/en/mod/mpm_common.html#sendbuffersize

Note: For other Webserver with php-cgi modes, read the relevant Webserver manual to find similar configurations.

Now, PHP's ECHO will directly deliver the content to Apache. after PHP completes the execution, it will not wait until the content is sent to the client and quit directly. the content will be sent to the client by Apache after PHP processing is complete. this accelerates the ECHO execution efficiency.

For example, printf, print, file_put_contents ("php: // output ")... And so on. it's the same as ECHO.

Finally, it should be noted that, in this way, only the waiting time of the original ECHO is transferred to Apache, and the time when the client obtains the content is not actually reduced. it only accelerates the PHP processing process and advances the exit time of PHP, which can reduce the time occupied by PHP resources and indirectly increase the resource usage.

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.