Table of Contents
怎样提高php运行速度?,提高php运行速度?
Home php教程 php手册 怎样提高php运行速度?,提高php运行速度?

怎样提高php运行速度?,提高php运行速度?

Jun 13, 2016 am 08:41 AM
Running speed

怎样提高php运行速度?,提高php运行速度?

使用PHP的最大1个优势就是速度快。一般情况下,PHP总是具有足够的速度支持Web内容动态生成,许多时候甚至无法找出比它更快的方法。然而,当面对庞大的访问量、高负荷的应用、有限的带宽,以及其他各种带来性能瓶颈的因素时,就需要考虑怎样提高PHP的性能了。
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 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)

Why does C code run faster than Python? Why does C code run faster than Python? Sep 11, 2023 pm 12:45 PM

In this article, we will learn why C code runs faster than Python. Guido Van Rossum developed Python, one of the most famous programming languages. Python is popular among developers because of its clear syntax and simple code, even for novices. Learning Python is highly beneficial for those who are just starting their programming career. They can use Python programming training, blogs, videos, modules, and thousands of other resources to learn all aspects of this popular language. Upon completion, you will be able to perform modern development activities such as GUI development, web design, system administration, complex financial transactions or calculations, data science, visualization, and more.

How to adjust virtual memory in win10 system to improve running speed How to adjust virtual memory in win10 system to improve running speed Jul 01, 2023 pm 02:01 PM

How to adjust virtual memory in win10 system to improve running speed? As the usage of computers becomes higher and higher, we may sometimes encounter the situation of adjusting the virtual memory of the win10 system to improve the running speed. If we need to adjust the virtual memory of the win10 system to improve the running speed, how should we deal with win10? Does the system adjust virtual memory to improve running speed? Fix solution for adjusting virtual memory in win10 system to improve running speed 1. Click Start Menu – All Applications – Windows System – This Computer, right-click this computer, click “Properties” to proceed to the next step. 2. In the system window that opens, click "Advanced System Settings" on the left to proceed to the next step. 3. In

A simple guide to improve the running speed of win7 A simple guide to improve the running speed of win7 Dec 26, 2023 pm 06:57 PM

When we use the win7 system, we may feel that the system is running lag or not smoothly. At this time, we can optimize it in the system's advanced settings and turn off unnecessary animations and services to improve the running speed of win7. Let's take a look at it together. Let’s look at optimization methods. Win7 running speed optimization tutorial 1. First find the computer and open "Properties" 2. Find "Advanced System Settings" 3. Click "Settings" under Performance 4. Check "Customize" and uncheck unnecessary content, or Uncheck all. 5. Or check "Adjust for best performance" above

Optimize Linux system to increase speed Optimize Linux system to increase speed Jun 30, 2023 am 11:00 AM

How to solve the problem of slow system running speed in Linux systems. With the widespread application of Linux operating system, many users have reported that the system running speed is too slow during use. This kind of problem not only affects work efficiency, but also reduces the user's experience of the Linux system. Therefore, it is very important to solve the problem of slow running speed of Linux system. This article will introduce some common solutions to help users improve the running speed of the system and improve the overall user experience. Clean up useless temporary files and

What are the main factors that affect computer speed? What are the main factors that affect computer speed? Dec 07, 2020 am 10:45 AM

The main factors that affect the running speed of the computer are the main frequency of the central processor and the access cycle of the memory. The main frequency is the clock frequency of the CPU. Computer operations are distributed and executed under the control of the clock signal. Each clock signal cycle completes one step of operation. The main frequency reflects the speed of the CPU to a large extent.

Understand the role of Go language in the development of programming languages Understand the role of Go language in the development of programming languages Mar 29, 2024 pm 04:36 PM

Title: Exploring the status of Go language in the development of programming languages. With the vigorous development of information technology, programming languages ​​play a vital role as a tool for programmers to communicate with computers. Among many programming languages, Go (also known as Golang), as a relatively young programming language, has received widespread attention. This article will explore the status of Go language in the development of programming languages ​​from the aspects of historical origins, characteristics, and specific code examples. 1. History and background of Go language Go language was born in 2007 by Google

How to optimize the running speed of Linux system How to optimize the running speed of Linux system Jun 30, 2023 am 10:25 AM

How to deal with the problem of slow system running speed in Linux system. In the process of using Linux system, sometimes we will encounter the problem of slow system running speed. This will not only affect our work efficiency, but may also make the system unstable. So, how to deal with the slow system running problem in Linux system? Some common solutions and optimization strategies are introduced below. Clear Temporary Files and Cache Temporary files and cache in Linux systems can take up a lot of hard drive space and slow down the system. we can

Does Go have typical programming language characteristics? Does Go have typical programming language characteristics? Mar 28, 2024 pm 03:32 PM

As a programming language designed to solve concurrency, performance, and scale issues, Go language is designed to have the characteristics of many typical programming languages. This article will analyze the characteristics of Go language from the perspective of several typical programming language characteristics, with corresponding code examples. 1. Static typing Static typing means that the programming language requires the type of variables to be determined at compile time. Go language is also a statically typed language. Here is a sample code showing static type declaration in Go language: packagemainimport

See all articles