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
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
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
$ 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
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
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
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