请注意,这快得多,并且在已知数组大小时应该使用。以下是一些非常基本的基准测试
<?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;
}
?>
输出
测试大小:1000
Array():0.00046396255493164
SplArray():0.00023293495178223
测试大小:2000
Array():0.00057101249694824
SplArray():0.0003058910369873
测试大小:4000
Array():0.0015869140625
SplArray():0.00086307525634766
测试大小:8000
Array():0.0024251937866211
SplArray():0.00211501121521
测试大小:16000
Array():0.0057680606842041
SplArray():0.0041120052337646
测试大小:32000
Array():0.011334896087646
SplArray():0.007631778717041
测试大小:64000
Array():0.021990060806274
SplArray():0.013560056686401
测试大小:128000
Array():0.053267002105713
SplArray():0.030976057052612
测试大小:256000
Array():0.10280108451843
SplArray():0.056283950805664
测试大小:512000
Array():0.20657992362976
SplArray():0.11510300636292
测试大小:1024000
Array():0.4138810634613
SplArray():0.21826505661011
测试大小:2048000
Array():0.85640096664429
SplArray():0.46247816085815
测试大小:4096000
Array():1.7242450714111
SplArray():0.95304894447327
测试大小:8192000
Array():3.448086977005
SplArray():1.96746301651