请注意,这速度要快得多,并且在已知数组大小的情况下应该使用。以下是一些非常基本的基准测试
<?php
for($size = 1000; $size < 50000000; $size *= 2) {
echo PHP_EOL . "Testing size: $size" . PHP_EOL;
for($s = microtime(true), $container = Array(), $i = 0; $i < $size; $i++) $container[$i] = NULL;
echo "Array(): " . (microtime(true) - $s) . PHP_EOL;
for($s = microtime(true), $container = new SplFixedArray($size), $i = 0; $i < $size; $i++) $container[$i] = NULL;
echo "SplArray(): " . (microtime(true) - $s) . PHP_EOL;
}
?>
输出
Testing size: 1000
Array(): 0.00046396255493164
SplArray(): 0.00023293495178223
Testing size: 2000
Array(): 0.00057101249694824
SplArray(): 0.0003058910369873
Testing size: 4000
Array(): 0.0015869140625
SplArray(): 0.00086307525634766
Testing size: 8000
Array(): 0.0024251937866211
SplArray(): 0.00211501121521
Testing size: 16000
Array(): 0.0057680606842041
SplArray(): 0.0041120052337646
Testing size: 32000
Array(): 0.011334896087646
SplArray(): 0.007631778717041
Testing size: 64000
Array(): 0.021990060806274
SplArray(): 0.013560056686401
Testing size: 128000
Array(): 0.053267002105713
SplArray(): 0.030976057052612
Testing size: 256000
Array(): 0.10280108451843
SplArray(): 0.056283950805664
Testing size: 512000
Array(): 0.20657992362976
SplArray(): 0.11510300636292
Testing size: 1024000
Array(): 0.4138810634613
SplArray(): 0.21826505661011
Testing size: 2048000
Array(): 0.85640096664429
SplArray(): 0.46247816085815
Testing size: 4096000
Array(): 1.7242450714111
SplArray(): 0.95304894447327
Testing size: 8192000
Array(): 3.448086977005
SplArray(): 1.96746301651