Home php教程 php手册 提高php运行速度的一些小技巧分享

提高php运行速度的一些小技巧分享

Jun 13, 2016 am 11:59 AM
php one code optimization share and clean Skill improve clear of run conduct speed

1、代码优化

代码优化不仅仅是写出干净和清晰的代码,而是对代码进行一定的简化。可以使用Zend Optimizer来自动帮助完成这些繁杂的工作。Zend Optimizer可以从Zend Technologies的网站http://www.zend.com/免费得到,但必须同意它的许可约定,因为它不是以GPL方式发行的。它的原理很简单,即通过检测Zend引擎产生的中间代码,并对它进行优化,从而获得更高的执行速度。
在使用了Zend Optimizer后,复杂的PHP源程序的执行效率马上会得到显著提高,缺点是优化后的代码可读性下降,给代码修改带来困难。

Zend Optimizer的安装方法非常简单,只要根据用户使用的平台,下载相关的预编译版本,把下面2行代码加入到php.ini文件中,重新启动Web 服务器就行了:
zend_optimizer.optimization_level=15
zend_extension=″/path/to/ZendOptimizer.so″
zend_loader.enable=Off

额外增加的第三行代码是可选的,因为禁用zend_loader将会使优化速度更快。需要注意的是,只有在不使用Zend Encoder Runtime的时候,才可以禁用zend_loader。

2、使用缓存

如果PHP程序的规模很大,那么提高速度的办法就是使用缓存。现在已经有许多缓存方案可供选择,其中包括Zend Cache、APC和Afterburner Cache。

上面这几种都是“缓存模块”(caching modules)。第一次调用PHP文件时,缓存模块从PHP源代码生成一些中间代码,并把这些中间代码存储在Web服务器的内存中。以后再调用这些文件时,就可以直接使用内存中“编译”过的代码。这种方法确实能够改善应用的性能,因为它使得磁盘访问量减低到了最少的程度(代码已经读取和解析),代码直接在内存中运行,使得服务器响应请求的速度大大提高。

当然,缓存模块还会监视PHP源文件的变化,必要时会重新缓存页面,从而防止用户得到的页面仍旧由过时的PHP代码生成。由于缓存模块能够明显地降低服务器的负载,提高PHP应用的响应效率,因此它们非常适合于负载较大的网站使用。

Zend Cache是Zend Technologies公司开发的商业软件。在第一次运行后,PHP页面的运行速度立刻会有很大的提高,服务器的空闲资源也更多了。缺点是它不是免费的,但性价比还是很高的。

Afterburner Cache是Bware Technologies公司开发的免费缓存模块。功能与Zend Cache基本一样,但提高性能方面比不上Zend Cache。

APC(Alternative PHP Cache)是由Community Connect公司开发的另一种免费缓存模块,目前版本是2.0.4,可以从http://pecl.php.net/package/APC获得。对于产品应用来说,它的性能很稳定,而且也能在很大程度上提高响应请求的速度。

3、压缩网页内容

影响站点的访问速度还有1个重要因素,那就是下载速度。解决的办法就是压缩网页内容。对于纯文本内容而言,HTTP压缩技术可压缩至原大小的40%以下,从而提供60%以上的数据传输节约。虽然Web服务器会因为压缩导致CPU占用的略微上升,但可以节约大量用于传输的网络IO。

根据IETF规范,大部分浏览器都支持使用gzip压缩算法进行内容压缩。也就是说,可以先用gzip压缩网页内容,然后发送到客户端浏览器,浏览器在接收的时候会自动解压数据,再显示页面。这个过程对用户来说,是完全透明的。同样,压缩Web页面的内容也有不同的方法。

Mod_gzip是1种开放源代码的、标准的Apache模块,也叫互联网内容加速模块。可以将它和Apache一起编译,也可以作为DSO使用。相对于普通的浏览过程,它可以节省40%左右的流量。Mod_gzip不仅可以压缩静态的内容,如HTML、XML,而且对动态生成的,包括SQL、Java、WML、VRML等产生的内容,在服务器端进行实时压缩并传输,其压缩效率惊人,一般都为60%~85%。

压缩动态网页的内容,还可以使用class.gzip来对.php文件编码,class.gzip通过在PHP脚本的开头和结尾调用它的一些函数来压缩网页内容。如果整个站点都需要这样的压缩,可以在php.ini文件中的auto_prepend和auto_append中调用这些函数,但是会占用一定的系统开销。

PHP4.0.4推出了1种新的输出缓冲的处理手段—ob_gzhandler,它的作用和class.gzip完全一样,区别是可以直接把它加到php.ini 文件中,语法如下:

output_handler = ob_gzhandler;

这样将激活PHP的输出缓冲功能,并在发送内容前进行压缩。如果不想在这里设置,只在需要的地方才改变这个默认设置(不压缩),只要在需要压缩的PHP源程序目录中,修改一下.htaccess文件就行了,语法如下:
php_value output_handler ob_gzhandler

或者直接在PHP代码中调用它:

ob_start("ob_gzhandler");
输出缓冲的效果确实很理想,并且不会为服务器带来额外的系统开销。要注意的一点是Netscape Communicator不支持图像的压缩。因此除非知道访问者都使用Internet Explorer,否则必须禁止压缩jpeg和gif图象。

4 其它技巧

在编程时,使用一些小技巧也可以加快PHP的运行速度:
(1)用i+=1代替i=i+1,既符合c/c++的习惯,效率相对还更高。
(2)尽可能使用PHP内部函数。
(3)能使用单引号字符串时,尽量使用单引号字符串。单引号字符串的效率要高于双引号字符串。
(4)用foreach代替while遍历数组,foreach的效率明显高于while循环,而且不需要调用reset函数。
以上四种方法就是唐山网站建设总结的一些个人经验,希望对大家有帮助,转载请留个链接谢谢了!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

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: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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 and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

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 and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

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.

See all articles