1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php $count=100000; $arr=range(1,$count,1); $ts = microtime(true); for ($i = 0; $i <$count; $i++) { $arr[$i]=$i+1; } $time =microtime(true)-$ts; echo($time.PHP_EOL); //foreach $arr=range(1,$count,1); $ts = microtime(true); foreach ($arr as $key => $value) { $arr[$key] = $value + 1; } $time =microtime(true)-$ts; echo($time.PHP_EOL); ?> |
到底谁快,测试才知道,答案是for快
百度一堆说foreach快的,还给出测试结果,但不知道为啥我测试的和他们相反呢
难道我姿势不对?
未经允许不得转载:开心乐窝-乐在其中 » for 和foreach 到底谁快?