2013-04-30 85 views
0

大家都說SplFixedArray is faster than array()但是......SplFixedArray似乎比陣列()慢

<?php 
$max_el = 256; 

$a = array(); 
$c = new SplFixedArray($max_el); 
for ($i = 0; $i < $max_el; $i++) { 
    $a[$i] = $c[$i] = mt_rand(0, 1000); 
} 

$start = microtime(true); 
for ($j = 0; $j < 100000; $j++) { 
    for ($i = 0; $i < $max_el; $i++) { 
     $x = $a[$i]; 
    } 
} 
$elapsed0 = microtime(true) - $start; 

$start = microtime(true); 
for ($j = 0; $j < 100000; $j++) { 
    for ($i = 0; $i < $max_el; $i++) { 
     $x = $c[$i]; 
    } 
} 
$elapsed2 = microtime(true) - $start; 

echo "Elapsed time: $elapsed0\r\n"; 
echo "Elapsed time: $elapsed2"; 

,它的輸出如下:

Elapsed time: 1.3631780147552 
Elapsed time: 2.0408799648285 

因此,與256個元素陣列()似乎更快。

這裏是我的結果與1024個元素:

Elapsed time: 5.6066889762878 
Elapsed time: 7.7747831344604 

另一個勝利數組()。

隨着10個* 1024個元素:

Elapsed time: 57.000520944595 
Elapsed time: 88.719652891159 

道具,再次,對陣列()。

確切地說,當SplFixedArray()應該更快?

回答