note 100582 added to class.splfixedarray

From: Date: Mon, 25 Oct 2010 12:38:58 +0000
Subject: note 100582 added to class.splfixedarray
Groups: php.notes 
Request: Send a blank email to php-notes+get-173431@lists.php.net to get a copy of this message
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. ---- Server IP: 69.147.83.197 Probable Submitter: 93.195.76.211 ---- Manual Page -- http://www.php.net/manual/en/class.splfixedarray.php Edit -- https://master.php.net/note/edit/100582 Del: integrated -- https://master.php.net/note/delete/100582/integrated Del: useless -- https://master.php.net/note/delete/100582/useless Del: bad code -- https://master.php.net/note/delete/100582/bad+code Del: spam -- https://master.php.net/note/delete/100582/spam Del: non-english -- https://master.php.net/note/delete/100582/non-english Del: in docs -- https://master.php.net/note/delete/100582/in+docs Del: other reasons-- https://master.php.net/note/delete/100582 Reject -- https://master.php.net/note/reject/100582 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173431) next »