Created
August 10, 2011 11:05
-
-
Save Thinkscape/1136563 to your computer and use it in GitHub Desktop.
Very simple performance comparison - arrays vs objects (PHP 5.3.6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
For information, you will get very different results if each Array/Object is different :
Array("name"=>"Adam$x","age"=>$x)
MyArrayObject("Adam$x", $x)
A quick test for PHP 7.2 https://gist.github.com/charrondev/be56ef53e0408b959eff6d2d911e8c1e
Type | Memory Usage | Real execution Time |
---|---|---|
Arrays | 46,191,568 | 0m0.137s |
Classes | 20,537,808 | 0m0.129s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://3v4l.org/30jdF/perf#tabs
https://3v4l.org/Qvl4W/perf#tabs
https://3v4l.org/DQ5jG/perf#tabs