夕口技術錄

專業上的小常識,備而用之~

[PHP][if-else] – 測試三元運算符與 if else 的運算速度

<?php
/**
* 以微秒为单位
* @return float
*/
function getMicrotime()
{
list($usec, $sec) = explode( , microtime());
return ((float)$usec + (float)$sec);
}


$fStartTime = getMicrotime();

for ($i=0; $i<100000; $i++){
$a = b;
if ($a==b) {
$a=c;
}
else{
$a=d;
}
}

$fEndTime = getMicrotime();

print $fEndTime $fStartTime;

print<br>;

$fStartTime = getMicrotime();

for ($i=0; $i<100000; $i++){
$a =
b;
$a = ($a==
b) ? c : ‘d;
}

$fEndTime = getMicrotime();

print $fEndTime $fStartTime;

?>
结果如下
0.133780002594  <- if else
0.106215000153  <- 三元
三元運算符比 if (){}else{} 快一些

2 responses to “[PHP][if-else] – 測試三元運算符與 if else 的運算速度

  1. 夕口 2010/06/28 於 22:48:48

    isset($field[“notnull"]) ? " NOT NULL" : “")

  2. 夕口 2010/06/28 於 22:48:48

    isset($field[“notnull"]) ? " NOT NULL" : “")

發表留言