php效率优化(原创)

1.检索一个数据存不存在。用in_array();不用循环….数据来源于数据库。

测试用例:实际项目。检索一个人是不是定理员。

$s =Array
(
    0 => Array
        (
            “USER_PASSPORT_ID” => 534589949
        ),
    1 => Array
        (
            “USER_PASSPORT_ID” => 534589959
        ),
      2 => Array
        (
            “USER_PASSPORT_ID” => 534589969
        ),
      3 => Array
        (
            “USER_PASSPORT_ID” => 534589979
        ),
      4 => Array
        (
            “USER_PASSPORT_ID” => 534589989
        )

);
$t =microtime(true);
if(in_array(array(“USER_PASSPORT_ID”=>534589949),$s)){
echo “good”;
}
$t2 =microtime(true);
echo ($t2-$t)*100000000;

$t =microtime(true);
foreach($s as $value){
foreach($value as $id){
   if($id==534589949){
    echo “yes”;
   }
}
}
$t2 =microtime(true);
echo ($t2-$t)*100000000;

?>

执行结果:

good4696.8460083yes1382.82775879

才五条数据就有了四倍的差距

Tags: