我运行了帖子末尾附带的基准测试。 为了比较这些方法:
count($arr) == 0
:使用 count 函数
empty($arr)
:使用 empty 函数
$arr == []
:进行比较操作
(bool) $arr
:强制转换类型
然后得到了以下结果。
Contents \method | count | empty | comp | cast |
------------------|--------------|--------------|--------------|--------------|
Empty |||| 1.051795 |
Uniform || 1.047339 |||
Integer |||| 1.051138 |
String || 1.049148 |||
Mixed |||| 1.064111 |
Associative || 1.053642 |||
------------------|--------------|--------------|--------------|--------------|
Total || 6.368568 |||
使用空值和强制转换为布尔类型之间的区别微不足道。我运行过这个测试多次,它们似乎基本上是等价的。数组的内容似乎不起显着作用。两者产生相反的结果,但逻辑否定几乎足以使强制转换大部分时间胜出,因此我个人更喜欢在任何情况下都使用empty来提高可读性。
<?php
$nt = 90000000;
$arr0 = [];
$arr1 = [];
$arr2 = [];
$arr3 = [];
$arr4 = [];
$arr5 = [];
for ($i = 0; $i < 500000; $i++) {
$arr1[] = 0;
$arr2[] = $i;
$arr3[] = md5($i);
$arr4[] = $i % 2 ? $i : md5($i);
$arr5[md5($i)] = $i;
}
$t00 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr0) == 0;
}
$t01 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr0);
}
$t02 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr0 == [];
}
$t03 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr0;
}
$t04 = microtime(true);
$t10 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr1) == 0;
}
$t11 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr1);
}
$t12 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr1 == [];
}
$t13 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr1;
}
$t14 = microtime(true);
$t20 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr2) == 0;
}
$t21 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr2);
}
$t22 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr2 == [];
}
$t23 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr2;
}
$t24 = microtime(true);
$t30 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr3) == 0;
}
$t31 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr3);
}
$t32 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr3 == [];
}
$t33 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr3;
}
$t34 = microtime(true);
$t40 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr4) == 0;
}
$t41 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr4);
}
$t42 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr4 == [];
}
$t43 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr4;
}
$t44 = microtime(true);
$t50 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr5) == 0;
}
$t51 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr5);
}
$t52 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr5 == [];
}
$t53 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr5;
}
$t54 = microtime(true);
$t60 = $t00 + $t10 + $t20 + $t30 + $t40 + $t50;
$t61 = $t01 + $t11 + $t21 + $t31 + $t41 + $t51;
$t62 = $t02 + $t12 + $t22 + $t32 + $t42 + $t52;
$t63 = $t03 + $t13 + $t23 + $t33 + $t43 + $t53;
$t64 = $t04 + $t14 + $t24 + $t34 + $t44 + $t54;
$ts0[1] = number_format(round($t01 - $t00, 6), 6);
$ts0[2] = number_format(round($t02 - $t01, 6), 6);
$ts0[3] = number_format(round($t03 - $t02, 6), 6);
$ts0[4] = number_format(round($t04 - $t03, 6), 6);
$min_idx = array_keys($ts0, min($ts0))[0];
foreach ($ts0 as $idx => $val) {
if ($idx == $min_idx) {
$ts0[$idx] = " $val ";
} else {
$ts0[$idx] = "/* $val */";
}
}
$ts1[1] = number_format(round($t11 - $t10, 6), 6);
$ts1[2] = number_format(round($t12 - $t11, 6), 6);
$ts1[3] = number_format(round($t13 - $t12, 6), 6);
$ts1[4] = number_format(round($t14 - $t13, 6), 6);
$min_idx = array_keys($ts1, min($ts1))[0];
foreach ($ts1 as $idx => $val) {
if ($idx == $min_idx) {
$ts1[$idx] = " $val ";
} else {
$ts1[$idx] = "/* $val */";
}
}
$ts2[1] = number_format(round($t21 - $t20, 6), 6);
$ts2[2] = number_format(round($t22 - $t21, 6), 6);
$ts2[3] = number_format(round($t23 - $t22, 6), 6);
$ts2[4] = number_format(round($t24 - $t23, 6), 6);
$min_idx = array_keys($ts2, min($ts2))[0];
foreach ($ts2 as $idx => $val) {
if ($idx == $min_idx) {
$ts2[$idx] = " $val ";
} else {
$ts2[$idx] = "/* $val */";
}
}
$ts3[1] = number_format(round($t31 - $t30, 6), 6);
$ts3[2] = number_format(round($t32 - $t31, 6), 6);
$ts3[3] = number_format(round($t33 - $t32, 6), 6);
$ts3[4] = number_format(round($t34 - $t33, 6), 6);
$min_idx = array_keys($ts3, min($ts3))[0];
foreach ($ts3 as $idx => $val) {
if ($idx == $min_idx) {
$ts3[$idx] = " $val ";
} else {
$ts3[$idx] = "/* $val */";
}
}
$ts4[1] = number_format(round($t41 - $t40, 6), 6);
$ts4[2] = number_format(round($t42 - $t41, 6), 6);
$ts4[3] = number_format(round($t43 - $t42, 6), 6);
$ts4[4] = number_format(round($t44 - $t43, 6), 6);
$min_idx = array_keys($ts4, min($ts4))[0];
foreach ($ts4 as $idx => $val) {
if ($idx == $min_idx) {
$ts4[$idx] = " $val ";
} else {
$ts4[$idx] = "/* $val */";
}
}
$ts5[1] = number_format(round($t51 - $t50, 6), 6);
$ts5[2] = number_format(round($t52 - $t51, 6), 6);
$ts5[3] = number_format(round($t53 - $t52, 6), 6);
$ts5[4] = number_format(round($t54 - $t53, 6), 6);
$min_idx = array_keys($ts5, min($ts5))[0];
foreach ($ts5 as $idx => $val) {
if ($idx == $min_idx) {
$ts5[$idx] = " $val ";
} else {
$ts5[$idx] = "/* $val */";
}
}
$ts6[1] = number_format(round($t61 - $t60, 6), 6);
$ts6[2] = number_format(round($t62 - $t61, 6), 6);
$ts6[3] = number_format(round($t63 - $t62, 6), 6);
$ts6[4] = number_format(round($t64 - $t63, 6), 6);
$min_idx = array_keys($ts6, min($ts6))[0];
foreach ($ts6 as $idx => $val) {
if ($idx == $min_idx) {
$ts6[$idx] = " $val ";
} else {
$ts6[$idx] = "/* $val */";
}
}
echo " | count | empty | comp | cast |\n";
echo "-------------|--------------|--------------|--------------|--------------|\n";
echo " Empty |";
echo $ts0[1] . '|';
echo $ts0[2] . '|';
echo $ts0[3] . '|';
echo $ts0[4] . "|\n";
echo " Uniform |";
echo $ts1[1] . '|';
echo $ts1[2] . '|';
echo $ts1[3] . '|';
echo $ts1[4] . "|\n";
echo " Integer |";
echo $ts2[1] . '|';
echo $ts2[2] . '|';
echo $ts2[3] . '|';
echo $ts2[4] . "|\n";
echo " String |";
echo $ts3[1] . '|';
echo $ts3[2] . '|';
echo $ts3[3] . '|';
echo $ts3[4] . "|\n";
echo " Mixed |";
echo $ts4[1] . '|';
echo $ts4[2] . '|';
echo $ts4[3] . '|';
echo $ts4[4] . "|\n";
echo " Associative |";
echo $ts5[1] . '|';
echo $ts5[2] . '|';
echo $ts5[3] . '|';
echo $ts5[4] . "|\n";
echo "-------------|--------------|--------------|--------------|--------------|\n";
echo " Total |";
echo $ts6[1] . '|';
echo $ts6[2] . '|';
echo $ts6[3] . '|';
echo $ts6[4] . "|\n";
empty()
。 - Cobbyif(!isset($emptyarray))
的值为false
,但是if(empty($emptyarray))
的值为true
。这正是让我掉进坑里的地方。 - Kellen Stuartcount()
是一个存储值(O(1)
)...请注意,数组的大小与性能无关:https://dev59.com/uG455IYBdhLWcg3wD_sB和https://dev59.com/c2025IYBdhLWcg3wtofX。`count()`和`!empty()`都是对已声明数组不必要的调用。https://dev59.com/fXE95IYBdhLWcg3wp_qn#59958121 - mickmackusa