Home Backend Development PHP Tutorial 提升PHP执行速度全攻略(上)

提升PHP执行速度全攻略(上)

Jun 01, 2016 pm 02:28 PM

php的优点之一是速度很快,对于一般的网站应用,可以说是已经足够了。不过如果站点的访问量很高、带宽窄或者其它的因素令服务器产生性能瓶颈的时候,你可能得想想其它的办法来进一步提高PHP的速度了。这篇文章将从几个方面介绍如何做到这一点,从而令用户浏览的时候更加“爽”。

  代码优化


  在这里并不想再次告诉你
如何写更干净的代码,这一点我想每个人都清楚,在需要速度的时候,你可能已经在PHP源代码的优化上面做了不少的工作,这里所提出的是,这个烦琐的工作可以交由其它工具来完成。这就是Zend Optimizer,此程序可以从Zend Technologies的网站(http://www.zend.com/)免费得到。它的原理很简单,通过检测Zend引擎产生的中间代码,并且优化它来得到更高的执行速度。我认为优化代码是一项颇烦琐的工作,而且优化后的代码可能变得难以理解,尤其是当你放下该PHP程序一段时间后,突然间客户要求你做一些修改时,可能你自己也不懂了;-)。因此我建议你在PHP的源代码较为复杂的时候,用Zend Optimizer来做这个优化的工作,好处是它不会令你的代码变得复杂难懂。

  安装Zend Optimizer是非常简单的。只要根据你使用的平台,下载相关的预编译库,并且在你的php.ini中加入两行,重新启动你的web服务器就行了!

  zend_optimizer.optimization_level=15zend_extension="/path/to/ZendOptimizer.so" zend_loader.enable=Off

  你可能有点奇怪,不是说两行吗,怎么变成三行了。不过第三行是可选的,看来禁止这个zend_loader将会令优化的速度更快,因此不妨在你的php.ini文件中多加这一行。要注意的是:只有在你不使用Zend Encoder Runtime的时候,才可以禁止zend_loader,关于Zend Encoder Runtime,还会在下文提到。

  要更快吗?使用cache(缓冲)吧

  如果你的PHP应用还需要更快的速度,下一个办法是缓冲。要实现这一点,有几种不同的方式。我自己就试用过Zend Cache(评测版本),APC和Afterburner Cache。

  以上提到的都是“缓冲模块”。它们的原理都差不多,在php文件被首次请求的时候,通过将你的PHP源代码的中间代码存储在web服务器的内存中,对于以后同样的请求,都直接提供内存中的“编译”版本。由于它可以令磁盘的访问达到最小化,因此这个方法确实可以极大地提高PHP的性能。更为方便的是,当你的PHP源代码修改时,缓冲的模块可以察觉到这些变化,并且重新载入一样,因此你不必担心客户得到的是旧版本的程序。这些缓冲的模块确实不错,但是我应该选用哪一种呢?下面分别介绍一下:

  Zend Cache是Zend Technologies的一个商业化的产品(它也是免费为我们提供PHP引擎和Zend Optimizer的公司)。它确实不错。在首次运行后,你可以明显察觉到PHP的速度得到了很大的提高,服务器的空闲资源也更多了。缺点是你要付费购买它,但就性价比来说,还是非常值得的。

  Afterburner Cache是Bware Technologies(bwcache.bware.it)提供的免费缓冲模块。当前还只是beta版,它所做的工作看来与Zend Cache差不多,不过性能的提高就比不上Zend Cache,而且现有的版本不能和Zend Optimizer一起工作,不过它是免费的。

  APC(Alternative PHP Cache)是由Community Connect(apc.communityconnect.com)提供的另一个免费模块。它的工作很稳定,速度也有不少的提高,要注意的是,我还没有找到一个官方的测试数据,这些只是在我的应用上作测试,因此不能下一个结论

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles