(http://code.google.com/intl/zh-CN/speed/articles/optimizing-php.html) 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. 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. 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. 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. 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. 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, and therefore, slow scripts. In the following example, imagine if a malicious user had inserted 512KB worth of characters into a textarea field. This would result in 1MB of memory being used! There's no reason to copy the variable above. You can simply do this operation inline and avoid the extra memory consumption: 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. Instead of using a loop, you can combine the data into a single database query. The PHP engine allows both single-quotes and double-quotes for string variable encapsulation, but there are differences! Using double-quotes for strings tells the PHP engine to read the string contents and look for variables, and to replace them with their values. On long strings which contain no variables, that can result in poor performance. Changing the double-quotes to single-quotes prevents the PHP engine from parsing this string in an attempt to expand variables which, in this example, don't exist: Using switch/case statements rather than loose-typed if/else statements when testing against a single variable results in better performance, readability, and maintainability. It's important to note that using switch/case does a loose-comparison, and should be taken into consideration when being used. Instead, you can use switch/case to test against the value of PHP performance tips
Profile your code to pinpoint bottlenecks
Upgrade your version of PHP
Use caching
Use output buffering
Don't copy variables for no reason
$description = strip_tags($_POST['description']);
echo $description;echo strip_tags($_POST['description']);Avoid doing SQL queries within a loop
foreach ($userList as $user) {Produces:
$query = 'INSERT INTO users (first_name,last_name) VALUES("' . $user['first_name'] . '", "' . $user['last_name'] . '")';
mysql_query($query);
}INSERT INTO users (first_name,last_name) VALUES("John", "Doe")$userData = [];Produces:
foreach ($userList as $user) {
$userData[] = '("' . $user['first_name'] . '", "' . $user['last_name'] . '")';
}
$query = 'INSERT INTO users (first_name,last_name) VALUES' . implode(',', $iserData);
mysql_query($query);INSERT INTO users (first_name,last_name) VALUES("John", "Doe"),("Jane", "Doe")...Use single-quotes for long strings
$output = "This is the content for a very long article
which is a few hundred lines long
and goes on and on and on
...The End";$output = 'This is the content for a very long article
which is a few hundred lines long
and goes on and on and on
...The End';Use switch/case instead of if/else
if($_POST['action'] == 'add') {
addUser();
} elseif ($_POST['action'] == 'delete') {
deleteUser();
} elseif ($_POST['action'] == 'edit') {
editUser();
} else {
defaultAction();
}$_POST['action']:switch($_POST['action']) {
case 'add':
addUser();
break;
case 'delete':
deleteUser();
break;
case 'edit':
editUser();
break;
default:
defaultAction();
break;
}Additional resources
2009-07-06
Let's make the web faster
Labels: php
2009-04-30
订阅:
帖子 (Atom)

