php 常用方法效率比较

  1. 数组问题
    foreach与for 效率比较
    foreach 快于 for 这是大家都明白的。 不仅如此。如果真的用for ,你这样写是最好的
    for( i=0, i = 0 , j=count( array); a r r a y ) ; i< j; j ; i++){
    }
    前面说了,数组用来做字串拼接,会慢,因为,你走了两循环。但很多操作,如果能用数组协助完成,则会很快。
    比如:array_map(‘trim’,$array)肯定比你写for,foreach要快很多。
    能先用explode拆成数组,最好不要在for循环中使用strpos.
    in_array函数的效率问题。如果in_array频繁使用,而数组很大,建议将这个数组排序,然后,用fast_in_array
    这是PHP手册中的用户添加的函数。(注:有待测试结果,小数组,in_array还是快于它)
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];

示例:

header("content-type:text/html;charset=utf-8");
/**
 * array_walk 和 foreach, for 的效率的比较。
 * 我们要测试的是foreach, for, 和 array_walk的效率的问题。 
 */

//产生一个10000的一个数组。
$max = 10000;
$test_arr = range(0, $max);
$temp = 0;
//我们分别用三种方法测试求这些数加上1的值的时间。

// for 的方法
$t1 = microtime(true);
for ($i = 0; $i < $max; $i++) {
    $temp = $temp + 1;
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "就使用for, 没有对数组操作 花费: {$t}<br>";

$t1 = microtime(true);
for ($i = 0; $i < $max; $i++) {
    $test_arr[$i] = $test_arr[$i] + 1;
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用for 并且直接对数组进行了操作 花费: {$t}<br>";

$t1 = microtime(true);
for ($i = 0; $i < $max; $i++) {
    addOne($test_arr[$i]);
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用for 调用函数对数组操作 花费 : {$t}<br>";

$t1 = microtime(true);
foreach ($test_arr as $k => &$v) {
    $temp = $temp + 1;
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用 foreach 没有对数组操作 花费 : {$t}<br>";

$t1 = microtime(true);
foreach ($test_arr as $k => &$v) {
    $v = $v + 1;
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用 foreach 直接对数组操作 : {$t}<br>";

$t1 = microtime(true);
foreach ($test_arr as $k => &$v) {
    addOne($v);
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用 foreach 调用函数对数组操作 : {$t}<br>";

$t1 = microtime(true);
array_walk($test_arr, 'addOne');
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用 array_walk 花费 : {$t}<br>";

function addOne(&$item) {
    $item = $item + 1;
}

结果:
就使用for, 没有对数组操作 花费: 0.003000020980835
使用 foreach 没有对数组操作 花费 : 0.002000093460083

使用for 并且直接对数组进行了操作 花费: 0.0039999485015869
使用 foreach 直接对数组操作 : 0.0019998550415039

使用for 调用函数对数组操作 花费 : 0.53900003433228
使用 foreach 调用函数对数组操作 : 0.54800009727478

使用 array_walk 花费 : 0.56699991226196

  1. 字符串问题
    1) 字符串拼接
    字符中拼接 大于 数组的implode, 也快于sprintf
    示例:
   /** 
     * 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 = '';     
    for ($i = 300000; $i > 0; $i--) {     
        $str .= 'String concatenation. ';     
    }    

    $end = microtime_float();  
    echo("<br/> t i m e :" .  round( $end - $start ,2) ."<br/>");  

    $start=microtime_float();  

    // array join     
    $str = '';     
    $sArr = array();     
    for ($i = 300000; $i > 0; $i--) {     
        $sArr[] = 'String concatenation. ';     
    }     
    $str = implode('',$sArr);    

    $end = microtime_float();  
    echo("<br/> t i m e :" .  round( $end - $start ,2) ."<br/>");  

结果:
t i m e :0.12
t i m e :0.22

2)字符串替换
sprintf 快于 str_replace 快于 preg_replace 快于 strstr

3)字符串查找
strpos 快于 preg_match 快于 strstr 快于 ereg

4)字符串输出
echo 快于 print

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.';
  1. 函数问题
    使用正名函数,不要用函数的别名。别名在PHP中是用于PHP的推广(比如split,join是VB中有的函数,implode,explode则是正名函数),或用于向旧版本兼容。一般速度没有正名的快。
    eg.
    count 快于 sizeof
    is_integer 快于 is_int
    floatval 快于 doubleval
    implode 快于 join
    ini_set 快于 ini_alter
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值