日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

[译]Speeding up your PHP scripts

發(fā)布時間:2025/3/17 php 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [译]Speeding up your PHP scripts 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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代碼,這些年里我見過很多不同風格不同標準的代碼。然而大多時候他們還沒有進行過優(yōu)化。

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.
這次我想聊聊如何讓你的代碼更優(yōu)化,以降低服務(wù)器的負載。

?

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.

在數(shù)組中也推薦使用單引號,因為它要比雙引號和不加引號快。

?

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 命令中要用到連接,那就把它優(yōu)化成多個參數(shù)相連。

The print function can't handle multiple parameters so don't even try.

print函數(shù)不能處理多個參數(shù),所以,請別嘗試。

$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
循環(huán)

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.
不要在循環(huán)體內(nèi),而是在循環(huán)體外定義 count 變量。如果不這樣,你的計數(shù)函數(shù)每次循環(huán)都會調(diào)用。

$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.
有三種方式可以用;
你只要簡單的設(shè)置一下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_ 函數(shù)代替 preg_ 函數(shù)。

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.
盡量用這些函數(shù)來代替preg_函數(shù): 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是一種語言結(jié)構(gòu),而strlen是一個函數(shù)。


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.
他會告訴你腳本運行的耗時分析和哪個部分用了最多的時間。這讓你很簡單就能找到你代碼中的瓶頸。



總結(jié)

以上是生活随笔為你收集整理的[译]Speeding up your PHP scripts的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。