加速PHP的ECHO[转]
作者: Laruence本文地址: http://www.laruence.com/2011/02/13/1870.html 转载请注明出处
你也许注意到过, 在PHP中使用ECHO输出大段字符串的时候, 执行时间会明显的长, 也就会有朋友认为PHP的ECHO性能很差.
我在之前的文章中, 已经解释过了原因, 也希望能纠正”PHP的ECHO性能差”的这个误会.
然而之前的文章, 也仅仅是给出了原因, 并没有介绍如何避免这个问题, 在今天公司内的某个产品线(Apache with PHP)发现了一个问题, 有用户在短时间内大量发起下载请求, 导致http连接数和数据库连接数剧增,
而数据库连接数剧增的原因是因为数据库的连接是单列模式, 一直到请求处理结束, 才会释放数据库链接. 这样就有了一个问题, 如果请求处理时间过长, 就会造成大量的数据库链接存在.
而这个用户的网速很慢, 这也就意味着, ECHO的”性能”很差~, 下载时间很长~. 如下图所示:
ECHO执行示意图
这也就引出了今天我要谈的这个问题, 如何让ECHO变快, 让PHP的请求处理过程, 尽快结束…
我们知道, 之所以ECHO慢, 是在等待”写数据”成功返回, 那么一个比较简单的办法, 就是打开输出缓存,
编辑php.ini
output_buffering = 4096 //byte当然, 你也可以在脚本中, 显示的调用ob_start():
ob_start(); echo $huge_string; //其他的逻辑. ob_end_flush();这里, 有一个要注意的地方, ob_start将会开辟一块4096大小的buffer, 所以, 如果huge_string大于4096, 将不会起到加速的作用.
现在, 我们的ECHO就会”立即”执行成功, 返回. 因为数据暂时写到了我们的输出缓存当中. 如果buffer足够大, 那么内容会等到脚本的最后, 才一次性发送给客户端(严格来说, 是发送给WebServer).
但这样并不能解决我们今天遇到的这个问题, 因为这些数据到最后, 还是需要PHP去把它们发送给客户端(此时不考虑WebServer的Output buffer), 这个过程不结束, 请求不会关闭, PHP也不会执行DB的析构函数~
那么, 既然做梦, 那就再做大点, 我们可以使用Apache的输出缓存. 也就是改变成如下的执行流程:
加速ECHO示意图
假设, 我们的PHP要输出100K的数据, 那么, 我们Apache的的输出缓存就必须大于100K, 否则当Apache的输出缓存满了以后, 就会真正的发送给客户端, 而这个过程中, 当时执行的ECHO就会阻塞等待.
那么, 如何修改Apache的输出缓存呢? 我们可以在apache的配置文件中, 使用SendBufferSize配置指令:
SendBufferSize 4096 //注意是byt具体的SendBufferSize的说明, 参看http://httpd.apache.org/docs/2.0/en/mod/mpm_common.html#sendbuffersize
注: 其他的Webserver with php-cgi的模式, 请翻阅相关Webserver的手册, 寻找类似配置.
现在, PHP的ECHO, 将直接把内容交给Apache, PHP在执行完成后, 不再等待内容发送给客户端完成, 而直接退出. 而内容会在PHP处理完成以后, 由Apache发送给客户端. 从而加速了ECHO的执行效率.
废话一句: printf, print, file_put_contents(“php://output”)…等等, 和ECHO都是一样的.
最后要说明, 这样做, 只是把原来ECHO的等待时间, 转移给了Apache, 并没有真正的减少客户端获取到内容的时间. 它只是加速了PHP的处理过程, 提前了PHP的退出时机, 从而能减少PHP对资源的占用时间, 间接增加资源的占用率.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
