Skip to content

Instantly share code, notes, and snippets.

@Thinkscape
Created August 10, 2011 11:05
Show Gist options
  • 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
@Thinkscape
Copy link
Author

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).

@ivanlanin
Copy link

Thanks! I need this test to find out which approach is better for my codes: array or stdobject.

@JoshuaKissoon
Copy link

In php > 5.4, this is completely different. See https://gist.github.com/nikic/5015323

@sc0ttkclark
Copy link

From PHP 5.6.12

# php -v
PHP 5.6.12 (cli) (built: Aug  6 2015 17:14:56) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

### PHP Array memory usage ###
# echo '<?php $s = array(); for($x=0;$x<1000;$x++){ $s[] = array("name"=>"Adam","age"=>35); }; echo memory_get_peak_usage(); ' | php
807336

### PHP ArrayObject() memory usage (no properties defined) ###
# 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
1144440

### PHP MyArrayObject() memory usage (properties defined) ###
# echo '<?php $s = array(); class MyArrayObject{ public $name, $age; public function __construct( $name, $age ) { $this->name = $name; $this->age = $age; } } for( $x=0;$x<1000;$x++){ $o = new MyArrayObject( 'Adam', 35 );  $s[] = $o;} echo memory_get_peak_usage(); ' | php
583600


### PHP Array memory usage + time (no properties defined) ###
# time echo '<?php $s = array(); for($x=0;$x<100000;$x++){ $s[] = array("name"=>"Adam","age"=>35); }; echo memory_get_peak_usage(); ' | php
58871792
real    0m0.193s
user    0m0.087s
sys 0m0.107s

### PHP ArrayObject() memory usage + time (no properties defined) ###
# 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
100801296
real    0m0.320s
user    0m0.153s
sys 0m0.163s

### PHP MyArrayObject() memory usage + time (properties defined) ###
# time echo '<?php $s = array(); class MyArrayObject{ public $name, $age; public function __construct( $name, $age ) { $this->name = $name; $this->age = $age; } } for( $x=0;$x<100000;$x++){ $o = new MyArrayObject( 'Adam', 35 );  $s[] = $o;} echo memory_get_peak_usage(); ' | php
44004544
real    0m2.209s
user    0m1.007s
sys 0m1.190s

We can conclude the following at least in PHP 5.6:

  • Winner for Speed: Arrays are faster than objects (with undefined or defined properties)
  • Winner for Memory: Objects with defined properties use less memory, 50%+ less than objects with undefined properties, only slightly less than Arrays

Ordered Fastest to Slowest in Speed for 100,000 items:

  • 0.107s - Arrays
  • 0.163s - Objects with undefined properties
  • 1.190s - Objects with defined properties

Ordered Least to Most Memory Usage for 1,000 items:

  • ~583kb - Objects with defined properties
  • ~807kb - Arrays
  • ~1.1mb - Objects with undefined properties

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.

@joeyhub
Copy link

joeyhub commented Oct 5, 2015

You can sometimes create your own symbol tables for arrays as well.

@vejja
Copy link

vejja commented Jan 18, 2016

PHP 5.6

php -v
PHP 5.6.14-0+deb8u1 (cli) (built: Oct 4 2015 16:13:10)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

echo '<?php $s = array(); for($x=0;$x<1000;$x++){ $s[] = array("name"=>"Adam","age"=>35); }; echo memory_get_peak_usage(); ' | php
804616

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
1141704

echo '<?php $s = array(); class MyArrayObject{ public $name, $age; public function __construct( $name, $age ) { $this->name = $name; $this->age = $age; } } for( $x=0;$x<1000;$x++){ $o = new MyArrayObject("Adam", 35 ); $s[] = $o;} echo memory_get_peak_usage(); ' | php
545176

time echo '<?php $s = array(); for($x=0;$x<100000;$x++){ $s[] = array("name"=>"Adam","age"=>35); } echo memory_get_peak_usage(); ' | php
58869048

Timing #1 #2 #3 #4 #5
real 0m0.385s 0m0.383s 0m0.384s 0m0.383s 0m0.385s
user 0m0.272s 0m0.276s 0m0.288s 0m0.312s 0m0.304s
sys 0m0.112s 0m0.104s 0m0.092s 0m0.072s 0m0.080s

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
100798560

Timing #1 #2 #3 #4 #5
real 0m0.672s 0m0.666s 0m0.658s 0m0.662s 0m0.683s
user 0m0.524s 0m0.540s 0m0.544s 0m0.548s 0m0.540s
sys 0m0.144s 0m0.124s 0m0.116s 0m0.112s 0m0.140s

$ time echo '<?php $s = array(); class MyArrayObject{ public $name, $age; public function __construct( $name, $age ) { $this->name = $name; $this->age = $age; } } for( $x=0;$x<100000;$x++){ $o = new MyArrayObject("Adam", 35 ); $s[] = $o;} echo memory_get_peak_usage(); ' | php
40797616

Timing #1 #2 #3 #4 #5
real 0m0.451s 0m0.464s 0m0.453s 0m0.471s 0m0.461s
user 0m0.368s 0m0.404s 0m0.396s 0m0.416s 0m0.388s
sys 0m0.084s 0m0.060s 0m0.056s 0m0.052s 0m0.072s

PHP 7.0

php -v
PHP 7.0.2-1~dotdeb+8.1 (cli) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

echo '<?php $s = array(); for($x=0;$x<1000;$x++){ $s[] = array("name"=>"Adam","age"=>35); }; echo memory_get_peak_usage(); ' | php
760288

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
944248

echo '<?php $s = array(); class MyArrayObject{ public $name, $age; public function __construct( $name, $age ) { $this->name = $name; $this->age = $age; } } for( $x=0;$x<1000;$x++){ $o = new MyArrayObject("Adam", 35 ); $s[] = $o;} echo memory_get_peak_usage(); ' | php
465440

time echo '<?php $s = array(); for($x=0;$x<100000;$x++){ $s[] = array("name"=>"Adam","age"=>35); } echo memory_get_peak_usage(); ' | php
42145848

Timing #1 #2 #3 #4 #5
real 0m0.203s 0m0.193s 0m0.201s 0m0.200s 0m0.193s
user 0m0.136s 0m0.144s 0m0.148s 0m0.164s 0m0.140s
sys 0m0.068s 0m0.052s 0m0.056s 0m0.040s 0m0.064s

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
61586192

Timing #1 #2 #3 #4 #5
real 0m0.306s 0m0.288s 0m0.297s 0m0.309s 0m0.315s
user 0m0.236s 0m0.236s 0m0.256s 0m0.268s 0m0.244s
sys 0m0.072s 0m0.056s 0m0.044s 0m0.044s 0m0.072s

time echo '<?php $s = array(); class MyArrayObject{ public $name, $age; public function __construct( $name, $age ) { $this->name = $name; $this->age = $age; } } for( $x=0;$x<100000;$x++){ $o = new MyArrayObject("Adam", 35 ); $s[] = $o;} echo memory_get_peak_usage(); ' | php
13587384

Timing #1 #2 #3 #4 #5
real 0m0.212s 0m0.221s 0m0.237s 0m0.238s 0m0.217s
user 0m0.180s 0m0.172s 0m0.192s 0m0.180s 0m0.188s
sys 0m0.032s 0m0.044s 0m0.052s 0m0.056s 0m0.028s

@Adri74100
Copy link

For information, you will get very different results if each Array/Object is different :
Array("name"=>"Adam$x","age"=>$x)
MyArrayObject("Adam$x", $x)

@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