PHP技巧:優化動態網頁技術PHP程序的12條技巧
2024-05-04 23:02:51
供稿:網友
1、if a method can be static, declare it static. speed improvement is by a factor of 4.
1、如果一個函數可以聲明為靜態的,那么就用靜態的,速度是改進的一個因素4.
2、# avoid magic like __get, __set, __autoload
2、避免使用__get, __set, __autoload等魔術函數
3、require_once() is expensive
3、require_once()是相當昂貴的。
4、use full paths in includes and requires, less time spent on resolving the os paths.
4、用include和require時,盡量用全路徑,可以減少花在解決os路徑的時間
5、if you need to find out the time when the scrīpt started executing, $_server[’request_time’] is preferred to time()
5、如果你需要知道一個腳本的開始運行時間,用 $_server[’request_time’]比用time()要好
6、see if you can use strncasecmp, strpbrk and stripos instead of regex
6、如果可以用strncasecmp, strpbrk and stripos就別用正則
7、preg_replace is faster than str_replace, but strtr is faster than preg_replace by a factor of 4
7、preg_replace比str_replace要快,但是strtr比preg_replace更快
8、if the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments.
一些函數,比如字符串替換函數,接受數組和單個字符做為參數,并且如果你的參數列表不是很長,考慮寫一些冗余替換語句,每次傳送一個字符,代替一行代碼接受數組做為查找和替換的參數。
9、error suppression with @ is very slow.
9、禁止錯誤輸出符號@是非常慢的。
10、$row[’id’] is 7 times faster than $row[id]
10、、$row[’id’]比 $row[id]快7倍
11、error messages are expensive
11、錯誤信息是非常昂貴的
12、do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) the count() function gets called each time.
12、不要在循環內部使用函數,比如: for ($x=0; $x < count($array); $x)。count()函數在每次循環中都會被調用。
對于模板,你是否在用smarty?這可能是保證把最經常訪問的頁面緩存起來的最快的方法了。