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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

See all articles