1 <?php 2 header('Content-type:text/html;Charset=utf-8'); 3 4 $start_time = microtime(true); 5 $a = []; 6 for ($i=0;$i<100000;$i++) { 7 $a['a1'] = 'a1'; 8 $a['b1'] = 'b1'; 9 $a['c1'] = 'c1'; 10 $a['d1'] = 'd1'; 11 $a['e1'] = 'e1'; 12 $a['f1'] = 'f1'; 13 } 14 15 $end_time = microtime(true); 16 17 echo '共'.round($start_time-$end_time,3).'秒'; 18 echo "<br>"; 19 20 $start_time = microtime(true); 21 22 $b = []; 23 for ($i=0;$i<100000;$i++) { 24 $b = [ 25 'a1'=>'a1', 26 'b1'=>'b1', 27 'c1'=>'c1', 28 'd1'=>'d1', 29 'e1'=>'e1', 30 'f1'=>'f1' 31 ]; 32 } 33 34 $end_time = microtime(true); 35 36 echo '共'.round($start_time-$end_time,3).'秒'; 37 echo "<br>";
结果:
结论:
第一种方式更快