note 100582 deleted from class.splfixedarray by danbrown

From: Date: Mon, 25 Oct 2010 15:06:17 +0000
Subject: note 100582 deleted from class.splfixedarray by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173441@lists.php.net to get a copy of this message
Note Submitter: kdimi ---- nairbv has an error in it's benchmark code: <?php for($j=0,$container=array();$j < $elements; $j++ ) { $container[$i] = NULL; } ß> This creates an empty array of size zero and assigns on the whole loop only one entry on the array. The final size is only 1, not $elements. <?php for( $j=0,$container=new SplFixedArray($elements);$j<$elements;$j++) { $container[$i] = NULL; } ?> This creates an array of $elements size, still assigns only one value, but the final size is equal $elements. When correcting his code, the results show, that SplFixedArray has a better performance then nairbv's benchmark showed and is around $elements = 40 equal to array() while getting only better when higher. ------------ I was interested to see how fromArray() performs, as in some scripts it might be necessary to copy arrays to arrays. Running the following code shows that SplFixedArray creation from another array is unacceptable much slower then copying an array to another array: <?php $elements = 50; for($j=0,$array=array();$j<$elements;$j++) { $array[$j] = $j; } for($s = microtime(true), $i = 0; $i < 1000000; $i++) { $containerX = $array; } echo "Array(): " . (microtime(true) - $s) . PHP_EOL; for($s = microtime(true), $i = 0; $i < 1000000; $i++) { $containerY = SplFixedArray::fromArray($array); } echo "SplFixedArray(): " . (microtime(true) - $s) . PHP_EOL; ?> Conclusion: If you consider to use SplFixedArray for speed, do test if your scenario will really profit from using it or might suffer. It might be not always a faster solution. As randallgirard pointed out, access seems to be not faster, only creation times and as this benchmark shows only when creating an empty array of a fixed size.

« previous php.notes (#173441) next »