- 浏览: 849013 次
- 性别:
- 来自: lanzhou
文章分类
最新评论
-
liu346435400:
楼主讲了实话啊,中国程序员的现状,也是只见中国程序员拼死拼活的 ...
中国的程序员为什么这么辛苦 -
qw8226718:
国内ASP.NET下功能比较完善,优化比较好的Spacebui ...
国内外开源sns源码大全 -
dotjar:
敢问兰州的大哥,Prism 现在在12.04LTS上可用么?我 ...
最佳 Ubuntu 下 WebQQ 聊天体验 -
coralsea:
兄弟,卫星通信不是这么简单的,单向接收卫星广播信号不需要太大的 ...
Google 上网 -
txin0814:
我成功安装chrome frame后 在IE地址栏前加上cf: ...
IE中使用Google Chrome Frame运行HTML 5
PHP performance tips
Author: Eric Higgins, Google Webmaster
Recommended experience: Beginner to intermediate PHP knowledge
PHP is a very popular scripting language, used on many popular sites across the web. In this article, we hope to help you to improve the performance of your PHP scripts with some changes that you can make very quickly and painlessly. Please keep in mind that your own performance gains may vary greatly, depending on which version of PHP you are running, your web server environment, and the complexity of your code.
Profile your code to pinpoint bottlenecks
Hoare's dictum states that Premature optimization is the root of all evil , an important thing to keep in mind when trying to make your web sites faster. Before changing your code, you'll need to determine what is causing it to be slow. You may go through this guide, and many others on optimizing PHP, when the issue might instead be database-related or network-related. By profiling your PHP code , you can try to pinpoint bottlenecks.
Upgrade your version of PHP
The team of developers who maintain the PHP engine have made a number of significant performance improvements over the years. If your web server is still running an older version, such as PHP 3 or PHP 4, you may want to investigate upgrading before you try to optimize your code.
- Migrating from PHP 4 to PHP 5.0.x
- Migrating from PHP 5.0.x to PHP 5.1.x
- Migrating from PHP 5.1.x to PHP 5.2.x
Use caching
Making use of a caching module, such as Memcache , or a templating system which supports caching, such as Smarty , can help to improve the performance of your website by caching database results and rendered pages.
Use output buffering
PHP uses a memory buffer to store all of the data that your script tries to print. This buffer can make your pages seem slow, because your users have to wait for the buffer to fill up before it sends them any data. Fortunately, you can make some changes that will force PHP to flush the output buffers sooner, and more often, making your site feel faster to your users.
Avoid writing naive setters and getters
When
writing classes in PHP, you can save time and speed up your scripts by
working with object properties directly, rather than writing naive
setters and getters. In the following example, the dog
class uses the setName()
and getName()
methods for accessing the name
property.
class dog { public $name = '' ; public function setName ( $name ) { $this -> name = $name ; } public function getName () { return $this -> name ; } }
Notice that setName()
and getName()
do nothing more than store and return the name
property, respectively.
$rover = new dog (); $rover -> setName ( 'rover' ); echo $rover -> getName ();
Setting and calling the name
property directly can run up to 100% faster
, as well as cutting down on development time.
$rover = new dog (); $rover -> name = 'rover' ; echo $rover -> name ;
Don't copy variables for no reason
Sometimes PHP novices attempt to make their code "cleaner" by copying predefined variables to variables with shorter names before working with them. What this actually results in is doubled memory consumption (when the variable is altered), and therefore, slow scripts. In the following example, if a user had inserted 512KB worth of characters into a textarea field. This implementation would result in nearly 1MB of memory being used.
$description = strip_tags($_POST['description']); echo $description;
There's no reason to copy the variable above. You can simply do this operation inline and avoid the extra memory consumption:
echo strip_tags($_POST['description']);
Avoid doing SQL queries within a loop
A common mistake is placing a SQL query inside of a loop. This results in multiple round trips to the database, and significantly slower scripts. In the example below, you can change the loop to build a single SQL query and insert all of your users at once.
foreach ($userList as $user) { $query = 'INSERT INTO users (first_name,last_name) VALUES("' . $user['first_name'] . '", "' . $user['last_name'] . '")'; mysql_query($query); }
Produces:
INSERT INTO users (first_name,last_name) VALUES("John", "Doe")
Instead of using a loop, you can combine the data into a single database query.
$userData = array(); foreach ($userList as $user) { $userData[] = '("' . $user['first_name'] . '", "' . $user['last_name'] . '")'; } $query = 'INSERT INTO users (first_name,last_name) VALUES' . implode(',', $userData); mysql_query($query);
Produces:
INSERT INTO users (first_name,last_name) VALUES("John", "Doe"),("Jane", "Doe")...
发表评论
-
Creating PDF documents with PHP in 10 steps
2009-11-09 08:33 1608Step 1 - Install the PDFlib e ... -
26 Hints for Agile Software Development
2009-11-04 08:04 739I collect nuggets of wisdom on ... -
40个迹象表明你还是PHP菜鸟
2009-10-28 08:40 60340个迹象表明你还是PHP菜鸟 ... -
国外优秀开源PHP建站程序一览
2009-10-15 08:06 1329大量的PHP开源(开放源 ... -
十步让你成为一名优秀的Web开发人员
2009-10-15 08:03 843第一步:学好HTML HTML( ... -
What's Agile To You?
2009-10-09 08:34 791As a project manager, archite ... -
install Merb 1.0 on Windows
2009-10-05 17:25 924早期的Merb在Windows上安装是件很恶心的事,缺这少 ... -
WebWork敏捷开发尝试
2009-10-05 09:11 790WebWork是一个优秀的J2EE ... -
敏捷开发中常见的九大误解
2009-10-05 09:10 764一、敏捷是“一个” ... -
浅谈敏捷项目管理在软件开发中的应用
2009-10-05 09:09 1195一、使用传统项目管理技术管理软件开发项目的方法 按照 ... -
揭示PHP成功背后的秘密:PHP创始人访谈录
2009-10-01 12:47 919Rasmus Lerdorf可能是格陵兰最著名的电脑牛人,他1 ... -
GitHub: Speed matters
2009-09-29 22:25 868Impressions from the first arti ... -
PHP5 Database Iterators <2>
2009-09-29 22:15 1023Introducing the Db_Iterator Obj ... -
PHP5 Database Iterators <1>
2009-09-29 22:11 883One feature of PHP rarely seen ... -
REST-*组织
2009-09-29 11:10 849JBoss已在月初的JBoss世界大会上正式宣布了它的新项 ... -
Agile 2009 Conference Retrospective
2009-09-26 21:51 817A month has passed since Agile ... -
Bill Burke Discusses REST-*, SOA/ROA and REST
2009-09-26 21:49 980InfoQ's recent post on REST-* ... -
git is great 2
2009-09-26 15:11 1477Git梳妆我们可以利用Git的config命令或者直接编辑~/ ... -
git is great
2009-09-26 15:08 1666Git是一个分布式的版本控制工具,本篇文章从介绍Git开始,重 ... -
FindBugs 1.3.9发布了
2009-09-26 08:03 1379由马里兰大学Bill Pugh教 ...
相关推荐
《Packtpub Drupal 6 Performance Tips Feb 2010》是针对Drupal 6平台的一本性能优化指南,由Packtpub出版社于2010年2月发布。该书聚焦于提升Drupal网站的运行效率,减少加载时间,增强用户体验,并帮助管理员和...
This book contains tips, tricks, and techniques to make new and existing PHP applications much faster and less resource-hungry. Pro PHP Application Performance will help you understand all the ...
It’s packed with useful, real world hints and tips that you can use on your sites today. How to Optimize MySQL: Indexes, Slow Queries, Configuration by Bruno Škvorc How to Read Big Files with PHP ...
Through the tips and best practices in this book, you’ll be able to do more with less code and reduce bugs in your applications. Not only will you be able to boost your performance, but you will also...
Through the tips and best practices in this book, you’ll be able to do more with less code and reduce bugs in your applications. Not only will you be able to boost your performance, but you will ...
Drupal是一个开源的内容管理系统(CMS),而"Performance Tips"意味着这份资料将探讨如何提升Drupal 6版本的运行效率。 **Drupal 6性能优化知识点:** 1. **缓存管理**:Drupal 6中的缓存机制是提高性能的关键。...
Database\Oracle Internals - Tips Tricks and Techniques for DBAs.mobi Database\OReilly High Performance MySQL.mobi Database\OReilly SQL and Relational Theory 2nd.mobi Database\OReilly SQL Cookbook.mobi...
Table of Contents Introduction....................................................................................................13 Code Examples.........................................................
- **Linux® Debugging and Performance Tuning: Tips and Techniques**:介绍如何调试Linux系统并优化其性能。 - **Understanding AJAX: Using JavaScript to Create Rich Internet Applications**:深入探讨使用...
- Tips for continuing development and staying updated with Flask and related technologies. #### Style and Approach The book adopts a practical, hands-on approach, providing comprehensive ...
《Linux® Debugging and Performance Tuning: Tips and Techniques》 - **作者**:Steve Best - **出版时间**:2006年 - **内容**:本书专注于Linux下的调试和性能优化技巧,适合希望深入了解Linux内核及应用程序...
This power tip provides an explanation as well as some tips to get the most out of temp files. Backup and Restore Settings One of the staples of UltraEdit (and UEStudio) is its highly configurable ...
2.1.3 Checking Default Performance .....................................................24 2.2.1 OK, I’m In. Now What? .............................................25 2.2.2 Just Publish Something! .....