导读
上一篇文章提到count — Count all elements in an array, or something in an object,但是还有一个sizeof(),它是count的别名,为何会有一个别名出现呢?不会是写这个函数的人偏好C,而用了sizeof吧,决定一探究竟。
说明
以下测试将分作两个版本,一是一般数组大小 10000 个元素,二是大数组 100000 个元素,并且只采用cli方式执行。
详细
A 10000
<?php echo 'Using for loop of sizeof(), the upper limit is 10,000. Do pre_calc or not, here are the performance differents >> ---- '; $x = array_fill(0, 10000, 'a'); // 填充数组,数组长度为 1000, 值大小为 9.5367431640625MB // 提前计算好大小 function with_pre_calc() { global $x; $lst = microtime(true); // 循环开始时间 loop start time 浮点型 $size = sizeof($x); for ($i=0; $i<$size; $i++); return microtime(true) - $lst; } // 没有提前计算好大小 function without_pre_calc() { global $x; $lst = microtime(true); // 循环开始时间 loop start time 浮点型 for ($i = 0; $i < sizeof($x); $i++); return microtime(true) - $lst; } echo 'With_pre_calc, using time >> ' . with_pre_calc() . ' second' . ' ---- ' . 'Without_pre_calc, using time >> ' . without_pre_calc() . ' second' . ' ---- '; unset($x); echo 'Total execute the script, using time >> ' . (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) . ' second';
<?php echo 'Using for loop of count(), the upper limit is 10,000. Do pre_calc or not, here are the performance differents >> ---- '; $x = array_fill(0, 10000, 'a'); // 填充数组,数组长度为 1000, 值大小为 9.5367431640625MB // 提前计算好大小 function with_pre_calc() { global $x; $lst = microtime(true); // 循环开始时间 loop start time 浮点型 $size = count($x); for ($i=0; $i<$size; $i++); return microtime(true) - $lst; } // 没有提前计算好大小 function without_pre_calc() { global $x; $lst = microtime(true); // 循环开始时间 loop start time 浮点型 for ($i = 0; $i < count($x); $i++); return microtime(true) - $lst; } echo 'With_pre_calc, using time >> ' . with_pre_calc() . ' second' . ' ---- ' . 'Without_pre_calc, using time >> ' . without_pre_calc() . ' second' . ' ---- '; unset($x); echo 'Total execute the script, using time >> ' . (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) . ' second';
结果
Using for loop of sizeof(), the upper limit is 10,000. Do pre_calc or not, here are the performance differents >>
---- With_pre_calc, using time >> 0.0028781890869141 second
---- Without_pre_calc, using time >> 11.052927017212 second
---- Total execute the script, using time >> 11.059575080872 second
Using for loop of count(), the upper limit is 10,000. Do pre_calc or not, here are the performance differents >>
---- With_pre_calc, using time >> 0.0029749870300293 second
---- Without_pre_calc, using time >> 10.996887922287 second
---- Total execute the script, using time >> 11.003705024719 second
这似乎没有多大区别。
B 100000
(脚本中只是替换了数组大小)
结果(各运行十次,取均值)
Using for loop of sizeof(), the upper limit is 20,000. Do pre_calc or not, here are the performance differents >>
---- With_pre_calc, using time >> 0.0057840347290039 second
---- Without_pre_calc, using time >> 54.608668088913 second
---- Total execute the script, using time >> 54.619874000549 second
Using for loop of count(), the upper limit is 20,000. Do pre_calc or not, here are the performance differents >>
---- With_pre_calc, using time >> 0.005860902557373 second
---- Without_pre_calc, using time >> 55.447510004044 second
---- Total execute the script, using time >> 55.458760023117 second
结论
这么看来,他们是没有区别的。难道真的是习惯问题?