KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
There is a major performance issue when using in-object array's as a property versus using a global php array variable, why? To benchmark this problem I created the following benchmark that stores an increasingly larger array with an stdClass as a node, two tests were run one using an array property in a class the other a global array. The test code ini_set('memory_limit', '2250M'); class MyTest { public $storage = []; public function push(){ $this->storage[] = [new stdClass()]; } } echo "Testing Objects".PHP_EOL; for($size = 1000; $size < 5000000; $size *= 2) { $start = milliseconds(); for ($a=new MyTest(), $i=0;$i<$size;$i++) { $a->push(); } $end = milliseconds(); echo "Array Size $size".PHP_EOL; echo $end - $start . " milliseconds to perform".PHP_EOL; } echo "================".PHP_EOL; echo "Testing Array".PHP_EOL; for($size = 1000; $size < 5000000; $size *= 2) { $start = milliseconds(); for ($a=[], $i=0;$i<$size;$i++) { $a[] = [new stdClass()]; } $end = milliseconds(); echo "Array Size $size".PHP_EOL; echo $end - $start . " milliseconds to perform".PHP_EOL; } And the shocking results: Testing Objects Array Size 1000 2 milliseconds to perform Array Size 2000 3 milliseconds to perform Array Size 4000 6 milliseconds to perform Array Size 8000 12 milliseconds to perform Array Size 16000 35 milliseconds to perform Array Size 32000 97 milliseconds to perform Array Size 64000 246 milliseconds to perform Array Size 128000 677 milliseconds to perform Array Size 256000 2271 milliseconds to perform Array Size 512000 9244 milliseconds to perform Array Size 1024000 31186 milliseconds to perform Array Size 2048000 116123 milliseconds to perform Array Size 4096000 495588 milliseconds to perform ================ Testing Array Array Size 1000 1 milliseconds to perform Array Size 2000 2 milliseconds to perform
Tags (comma-separated)
Save Edits
Cancel