php测试数组使用引用和赋值的效率


测试结果: 1.当数组大于4左右时,引用的速度就快过了赋值; 2.当数组大于150左右时,引用快于赋值的速度能得到实际收益。 注意:测试在win系统下使用Apache/1.3.29 (Win32) PHP/4.3.6 测得 测试误差在5%-10%之间。 测试代码:

/** * * @author xiami * @version 0.1 beta * @copyright none */ class Debug { function startTimer() { global $starttime; $mtime = microtime (); $mtime = explode (’ ‘, $mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; } function endTimer() { global $starttime; $mtime = microtime (); $mtime = explode (’ ‘, $mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = round (($endtime - $starttime), 20); return $totaltime; } } $d = new Debug; $b = 0; for($i = 1;$i startTimer(); $array2 = &$array; $test1 = $d->endTimer(); $d->startTimer(); $array3 = $array; $test2 = $d->endTimer(); if ($test1 < $test2) { echo ‘
test ‘ . $i . ‘:’ . ($test2 - $test1); $b++; } } echo ‘
’ . ($i - $b) . ‘:’ . $b;

comments powered by Disqus