Heim  >  Artikel  >  Backend-Entwicklung  >  向PHP要效率——加速你的代码执行速度

向PHP要效率——加速你的代码执行速度

不言
不言Original
2018-05-17 15:53:542425Durchsuche

  向PHP要效率——加快你的代码执行速度

脚本语言效率都是低下的,PHP也不例外。而效率的高低实际上,都是在你的每一行代码中节省或浪费的。所以,这里向你讲解一些基本的效率常识,让你的代码执行速度快起来。
(注:看过N多版本的优化PHP代码的建议,觉得不系统,也不全面,这里只是给出平常极需要注意的一些。)

一、字符串问题

1、字符中拼接大于数组的implode, 也快于sprintf

你可以执行一下下面的代码:

<?php    
/**     
* Simple function to replicate PHP 5 behaviour     
*/    
function microtime_float()    
{        
list($usec, $sec) = explode(" ", microtime());        
return ((float)$usec + (float)$sec);    
}        
$start=microtime_float();        
// standard string append       
$str = &#39;&#39;;       
for ($i = 300000; $i > 0; $i--) 
{           
$str .= &#39;String concatenation. &#39;;       
}          
$end = microtime_float();    
echo("<br/> t i m e :" .  round( $end - $start ,2) ."<br/>");        
$start=microtime_float();        
// array join       
$str = &#39;&#39;;       
$sArr = array();       
for ($i = 300000; $i > 0; $i--) 
{           
$sArr[] = &#39;String concatenation. &#39;;       
}       
$str = implode(&#39;&#39;,$sArr);          
$end = microtime_float();    
echo("<br/> t i m e :" .  round( $end - $start ,2) ."<br/>");    
?>

我机器的输出结果是:

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 = &#39;John SMITH&#39;;? 
echo "Hello $foo, welcome on my blog.";? 
echo "Hello " . $foo . " welcome on my blog.";? 
echo &#39;Hello &#39; . $foo . &#39; welcome on my blog.&#39;;? 
echo &#39;Hello &#39;, $foo , &#39; welcome on my blog.&#39;;

我想,你能看得懂的,最后一个最快。

二、数组问题:

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:

<?php 
function fast_in_array($elem, $array) {    
$top = count($array) -1;    
$bot = 0;    
while($top >= $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, 我觉得,可以不管它。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:转载 PHP 数组(一)Nächster Artikel:UA识别有什么用?