Skip to content

Instantly share code, notes, and snippets.

@Thinkscape
Created August 10, 2011 11:05
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Thinkscape/1136563 to your computer and use it in GitHub Desktop.
Save Thinkscape/1136563 to your computer and use it in GitHub Desktop.
Very simple performance comparison - arrays vs objects (PHP 5.3.6)
# 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
@charrondev
Copy link

charrondev commented Nov 26, 2018

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