[译]Speeding up your PHP scripts
I've been coding in PHP ever since I was 13, over the years I have seen many different coding styles and standards being used. However most of them weren't optimised.
我從13歲就開始編寫PHP代碼,這些年里我見過很多不同風格不同標準的代碼。然而大多時候他們還沒有進行過優化。
This time I'd like to talk about different ways on how you can speed up your scripts by optimizing your code to reduce your server load.
這次我想聊聊如何讓你的代碼更優化,以降低服務器的負載。
?
Coding tips
編碼要點
Quotes
引號
Try using single quotes as much as possible, it's faster than double quotes because PHP searches for variables in text surrounded in double quotes.
盡可能的使用單引號,它比雙引號快,因為PHP會在雙引號包圍的字符串中搜索變量。
Using single quotes in arrays is also recommended since it's faster than calling it with double or no quotes.
在數組中也推薦使用單引號,因為它要比雙引號和不加引號快。
?
Echo VS. print
Echo 與 print
Echo is faster than print, if you're using concatenation in your echo command then you could optimise it further by using multiple parameters instead of concatenation.
Echo 比 print 快,盡可能的使用單引號,如果你在echo 命令中要用到連接,那就把它優化成多個參數相連。
The print function can't handle multiple parameters so don't even try.
print函數不能處理多個參數,所以,請別嘗試。
$name = 'zenk0';echo 'the user ', $name, ' has been selected for a special event.';
//slower and more widely used
echo 'The user ' . $name . ' has been selected for a special event.';
For loops
循環
Define your count variable before you start looping instead of in your loop. If you don't do this then your count function will be repeated everytime a loop happens.
不要在循環體內,而是在循環體外定義 count 變量。如果不這樣,你的計數函數每次循環都會調用。
$array = array('one', 'two', 'three');
$count = count($array);
//slow : for($i=0; $i < count($array); $i++)
for($i=0; $i < $count; $i++)
{
echo array[$i];
}
Simple conditionals
簡單條件判斷
It's better to use a switch statement instead of if/else if/else when you're using simple conditionals.
如果你的條件比較簡單,使用 switch 比用 if/else if/else 好多了。
Includes & requires
Includes 和 requires
There are several ways to gain some speed in this are; first of all drop the use of require_once it's much slower than include_once. Try to use full paths in your includes and requires, it'll spare the server some time on resolving the paths.
這兒有幾種方法可以加速: 首先,用require_once比include_once慢多了。盡量在你的包含和引用中用全路徑,這樣可以省掉解析路徑的開銷。
There are 3 ways of doing this;
You could just simply set the include path.
有三種方式可以用;
你只要簡單的設置一下include的路徑。
?
// Works as of PHP 4.3.0
set_include_path('/inc');
?
// Works in all PHP versions
ini_set('include_path', '/inc');
?
Or you could create a variable with the include path and concatenate it at the start of an include.
或者新建個用于包含的路徑變量,并把它放到include連接起來。
$include = '/home/user/www';
include ($include . '/library/loader.php');
?
As a third example; you could also just get the directory path from the current file.
第三個例子:你也可以只獲取當前文件的路徑。
$dir = dirname(__FILE__);
include ($dir . '/library/loader.php');
?
Handling strings
處理字符串
Try to make use of as much str_ functions as possible instead of turning to preg_ functions.
盡量使用 str_ 函數代替 preg_ 函數。
Str_replace is much faster than preg_replace if you're not making use of regex patterns. In turn strstr is faster than using str_replace.
要是你不用正則表達式,str_replace比preg_replace快多了。 但是strstr 比 str_replace快。
Try to make use of these functions instead of preg_ functions: strpos, strpbrk, strncasecmp.
盡量用這些函數來代替preg_函數: strpos, strpbrk, strncasecmp。
If you need to check ?if a string has a certain length it's better to use the isset trick than using strlen.
如果你需要檢查一個字符串的長度,巧妙的使用isset比strlen好多了。
$str = 'have a nice day';
//check if the string has more than 6 characters
//slow, checks if the string has less than 7 characters.
if(strlen($str) < 7)
//fast, if there is no seventh character set
if(!isset($str{6}))
This is because isset is a language construct, whereas strlen is a function that is looked up.
這是因為,isset是一種語言結構,而strlen是一個函數。
Ofcourse there are several tools than can help you; use a code profiler.
當然有一些工具可以幫忙:用 a code profiler。
They will tell you which how much time is spent parsing your script and which part requires the most time. ?This makes it easy to find bottlenecks in your code.
他會告訴你腳本運行的耗時分析和哪個部分用了最多的時間。這讓你很簡單就能找到你代碼中的瓶頸。
總結
以上是生活随笔為你收集整理的[译]Speeding up your PHP scripts的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Weblogic的classpath设置
- 下一篇: Oracle 9i 的安装(图解)