# php -v | |
PHP 5.3.6 (cli) (built: Mar 17 2011 20:58:15) | |
Copyright (c) 1997-2011 The PHP Group | |
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies | |
# echo '<?php $s = array(); for($x=0;$x<1000;$x++){ $s[] = array("name"=>"Adam","age"=>35); }; echo memory_get_peak_usage(); ' | php | |
655040 | |
# echo '<?php $s = array(); for($x=0;$x<1000;$x++){ $o = new ArrayObject; $o->name = "Adam"; $o->age = 35; $s[] = $o;} echo memory_get_peak_usage(); ' | php | |
887984 | |
# time echo '<?php $s = array(); for($x=0;$x<100000;$x++){ $o = new ArrayObject; $o->name = "Adam"; $o->age = 35; $s[] = $o;} echo memory_get_peak_usage(); ' | php | |
61010784 | |
real 0m1.448s | |
user 0m1.208s | |
sys 0m0.231s | |
# time echo '<?php $s = array(); for($x=0;$x<100000;$x++){ $s[] = array("name"=>"Adam","age"=>35); }; echo memory_get_peak_usage(); ' | php | |
33647944 | |
real 0m0.525s | |
user 0m0.429s | |
sys 0m0.092s | |
This comment has been minimized.
This comment has been minimized.
ivanlanin
commented
Dec 16, 2011
Thanks! I need this test to find out which approach is better for my codes: array or stdobject. |
This comment has been minimized.
This comment has been minimized.
JoshuaKissoon
commented
Aug 22, 2013
In php > 5.4, this is completely different. See https://gist.github.com/nikic/5015323 |
This comment has been minimized.
This comment has been minimized.
sc0ttkclark
commented
Aug 27, 2015
From PHP 5.6.12
We can conclude the following at least in PHP 5.6:
Ordered Fastest to Slowest in Speed for 100,000 items:
Ordered Least to Most Memory Usage for 1,000 items:
I ran these from Mac OSX terminal with local PHP, so the speeds aren't going to be the same as on a real server with ongoing load. |
This comment has been minimized.
This comment has been minimized.
joeyhub
commented
Oct 5, 2015
You can sometimes create your own symbol tables for arrays as well. |
This comment has been minimized.
This comment has been minimized.
vejja
commented
Jan 18, 2016
PHP 5.6
PHP 7.0
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Adri74100
commented
Jul 2, 2017
For information, you will get very different results if each Array/Object is different : |
This comment has been minimized.
This comment has been minimized.
charrondev
commented
Nov 26, 2018
•
A quick test for PHP 7.2 https://gist.github.com/charrondev/be56ef53e0408b959eff6d2d911e8c1e
|
This comment has been minimized.
Thinkscape commentedAug 10, 2011
Objects are up to twice slower at init and take up to double memory. It gets worse, as the above example is without autoloading and only using SPL ArrayObject which is somewhat lightweight (compiled-in).