4 回答
TA贡献1859条经验 获得超6个赞
=
是赋值运算符==
是比较运算符(检查两个变量是否具有相等的值)===
是相同的比较运算符(检查两个变量是否具有相同的值并且属于同一类型)。
TA贡献1795条经验 获得超7个赞
对于高级PHP用户,知道之间的差别==和===与问自己“是它更快地进行比较==或与===我敢肯定,这两个操作数是相同的类型?”
简短而一般的答案是:在这种情况下使用没有性能提升===,所以你应该使用==。
感兴趣的标杆它自己的那些,你可以使用下面的代码我写的ad-hoc和尝试不同的价值观$a和$b:
<?php
// CONFIGURATION
$cycles = 1000000;
$a = 'random string 1';
$b = 'random string 2';
// FUNCTIONS
function compare_two_equals($a, $b) {
if ($a == $b) {
return TRUE;
} else {
return FALSE;
}
}
function compare_three_equals($a, $b) {
if ($a === $b) {
return TRUE;
} else {
return FALSE;
}
}
// EXECUTION
$time = microtime(TRUE);
for ($count_a = 0; $count_a < $cycles; $count_a++) {
compare_two_equals($a, $b);
}
$time_two_a = microtime(TRUE) - $time;
$time = microtime(TRUE);
for ($count_a = 0; $count_a < $cycles; $count_a++) {
compare_three_equals($a, $b);
}
$time_three_a = microtime(TRUE) - $time;
$time = microtime(TRUE);
for ($count_a = 0; $count_a < $cycles; $count_a++) {
compare_two_equals($a, $b);
}
$time_two_b = microtime(TRUE) - $time;
$time = microtime(TRUE);
for ($count_a = 0; $count_a < $cycles; $count_a++) {
compare_three_equals($a, $b);
}
$time_three_b = microtime(TRUE) - $time;
$time = microtime(TRUE);
// RESULTS PRINTING
print "<br />\nCOMPARE == (FIRST TRY): " . number_format($time_two_a, 3) . " seconds";
print "<br />\nCOMPARE == (SECOND TRY): " . number_format($time_two_b, 3) . " seconds";
print "<br />\nCOMPARE === (FIRST TRY): " . number_format($time_three_a, 3) . " seconds";
print "<br />\nCOMPARE === (SECOND TRY): " . number_format($time_three_b, 3) . " seconds";
?>
注意:只有当每个“FIRST TRY”非常接近“SECOND TRY”时,比较才有效。如果它们明显不同,则意味着处理器在执行比较时忙于执行其他操作,因此结果不可靠并且应该再次运行基准测试。
- 4 回答
- 0 关注
- 440 浏览
添加回答
举报