向PHP要效率――加速你的代碼執行速度

來源:互聯網
上載者:User
向PHP要效率――加快你的代碼執行速度

指令碼語言效率都是低下的,PHP也不例外。而效率的高低實際上,都是在你的每一行代碼中節省或浪費的。所以,這裡向你講解一些基本的效率常識,讓你的代碼執行速度快起來。
(註:看過N多版本的最佳化PHP代碼的建議,覺得不系統,也不全面,這裡只是給出平常極需要注意的一些。)

一、字串問題

1、字元中拼接大於數組的implode, 也快於sprintf

你可以執行一下下面的代碼:

 0; $i--) {           $str .= 'String concatenation. ';       }          $end = microtime_float();    echo("
t i m e :" . round( $end - $start ,2) ."
"); $start=microtime_float(); // array join $str = ''; $sArr = array(); for ($i = 300000; $i > 0; $i--) { $sArr[] = 'String concatenation. '; } $str = implode('',$sArr); $end = microtime_float(); echo("
t i m e :" . round( $end - $start ,2) ."
"); ?>

?

我機器的輸出結果是:

t i m e :0.14

t i m e :0.25

2、字串替換

同時,如果不能拼接,才考慮替換。而替換方式,要按以下的優先方式考慮寫代碼:

sprintf 快於 str_replace 快於 preg_replace 快於 strstr

3、字串尋找,字串比較:

網上有人測試的結果是:

Results
ereg .956
preg_match .050
strstr .222
strpos .033
可見:
strpos 快於 preg_match 快於 strstr 快於 ereg
有人說,strstr快,但,preg_match_all肯定比for迴圈中的strstr快,如果能夠explode,則還要比preg_match_all快

3、字串輸出:
echo 快於 print, 這不用講了。 但是,如果將echo用到最快?

$foo = 'John SMITH';?
echo "Hello $foo, welcome on my blog.";?
echo "Hello " . $foo . " welcome on my blog.";?
echo 'Hello ' . $foo . ' welcome on my blog.';?
echo 'Hello ', $foo , ' welcome on my blog.';


我想,你能看得懂的,最後一個最快。


二、數組問題:

foreach 快於 for 這是大家都明白的。 不僅如此。如果真的用for ,你這樣寫是最好的

for($i=0,$j=count($array);$i<$j;$i++){
}

前面說了,數組用來做字串拼接,會慢,因為,你走了兩迴圈。但很多操作,如果能用數組協助完成,則會很快。
比如:array_mar('trim',$array)肯定比你寫for,foreach要快很多。
能先用explode拆成數組,最好不要在for迴圈中使用strpos.

in_array函數的效率問題。如果in_array頻繁使用,而數組很大,建議將這個數組排序,然後,用fast_in_array

這是PHP手冊中的使用者添加的函數。(註:有待測試結果,小數組,in_array還是快於它)
This function is five times faster than in_array(). It uses a binary search and should be able to be used as a direct replacement:

= $bot)    {       $p = floor(($top + $bot) / 2);       if ($array[$p] < $elem) $bot = $p + 1;       elseif ($array[$p] > $elem) $top = $p - 1;       else return TRUE;    }         return FALSE; } ?> 

?

用數組改變你的所有能改變的控制結構。這不僅包括三元運算子,還有:if,switch。這還有另一好處,那就是能培養你的軟編碼模式的思維。

Instead of

?? $class = $class == 'even' ? 'odd' : 'even'

we have

?? $flip = array('even' => 'odd', 'odd' => 'even');
?? $class = $flip[$class];
??
三、函數問題

使用正名函數,不要用函數的別名。別名在PHP中是用於PHP的推廣(比如split,join是VB中有的函數,implode,explode則是正名函數),或用於向舊版本相容。一般速度沒有正名的快。
??
count 快於 sizeof
is_integer 快於 is_int
floatval 快於 doubleval
implode 快於 join
ini_set 快於 ini_alter

當然,也有極個別的例外,比如:fputs 快於 fwrite, 我覺得,可以不管它。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.