PHP: Arrays vs. Objects
In a lot of cases arrays are used in PHP to store object-like information, like the results of a database query. I do this a lot too, but I kind of want to change things around to make use of VO's. I feel this makes a lot more sense, since most of the application I build are heavy OOP anyway, and I get all the added OOP benefits, like type-hinting, inheritance.. well, you know the deal.
I wanted to see what the differences would be in terms of memory consumption, so I set up the following test:
<?php
// first test simple associative arrays
$memory1 = xdebug_memory_usage( );
$data = array();
for($i=0;$i<1000;$i++) {
$data[] = array(
'property1' => md5(microtime()),
'property2' => md5(microtime()),
'property3' => md5(microtime()),
);
}
$array = xdebug_memory_usage()-$memory1 . "\n";
// Now do the same thing, but with a class..
class Test {
public $property1;
public $property2;
public $property3;
}
$data = array();
$memory1 = xdebug_memory_usage( );
for($i=0;$i<1000;$i++) {
$test = new Test();
$test->property1 = md5(microtime());
$test->property2 = md5(microtime());
$test->property3 = md5(microtime());
$data[] = $test;
}
$object = xdebug_memory_usage()-$memory1;
echo 'Arrays: ' . $array . "\n";
echo 'Objects: ' . $object;
?>
My results were
Arrays: 536596
Objects: 521932
I knew there was a good chance objects would take up less memory, because arrays need to store both the propertyname (or key) and value for every record, while the object only needs to store the values, because the propertynames are stored centrally in the class definition, what I didn't expect was that using arrays takes more than 20 times more memory. This is hardly an accurate formula, but it does tell you something.
Right, that was stupid.. I had my testing code wrong and I did the $data=array(); right after the second xdebug_memory_usage(). The actual conclusion here is that there's not much difference. I was hoping the objects would make a significant difference, but its minimal.
Comments
joel •
I just tried to reproduce this, but couldn't.I separated the code into two files, one for array test and the other for object test.
I used memory_get_peak_usage(true) since i don't have xdebug installed.
Both scripts reported peak memory usage of 1MB when running $i to 1000.
Increasing $i to 10000 resulted in memory usage for Array at 9MB and for Object ad 10MB.
So, obviously this requires more work. But, if you're going to store 10MB worth of data then you're probably doing something wrong.
The real benefit to using an Object would be having objects being passed by reference transparently. You have to be careful with an Array, since, although they are copied on write. Passing the Array around is okay, but as soon as you have to make a change to an element then the entire structure has to be copied. Not so with an object representation.
Joel
Evert •
Ahh that was stupid =P I reset the array after the second xdebug_memory_usage( );I'll update this now
Arnold Daniels •
The reason why there is little difference is because objects need to save the property name as well. Just try to assign a non existent property to the object:$test->prop_foo = 20;
echo $test->prop_foo;
Lukas Smith •
One thing to also consider is serialization speed, there arrays and objects are close in some situations, but arrays look much faster in others. Also things go back and forth between what writes and what reads faster:http://pooteeweet.org/blog/721
Evert •
Arnold,You're right .. I was just hoping since PHP knows about those properties, it would handle the defined properties slightly different.
Evert
Les •
To make the comparison between the use of arrays against objects is actually mute as there are numerous benefits that you have over arrays, whilst using objects.Any performance either way isn't here nor there; It's not something that you'd notice, to the point that making the switch from objects, to arrays would gain you anything significant.
Surely, there are other areas of application development that have bottlenecks that need to be addressed first, long before you would even contemplate this comparison?
I would think so :)
Evert •
Les,I totally agree.. I really do wanna make that switch.
I just wanted to see here if there were any other benefits, besides the obvious.
Evert
Travis Swicegood •
Just ran this test on a MacBook Pro and got a little better results than you did. At 1,000 entries it used about 10% less memory; at 100 entries the array had divided it's total memory by 10, however the object had dropped from 487k in memory to 2.6k. When I went up to 100,000 entries, the objects performed worse; they were about 10% above an array.From that, one could draw the conclusion on smaller datasets (< 1k), objects are beneficial in terms of memory usage.
Evert •
Thats an interesting result Travis,Interesting in the sense of, it doesn't mate any sense :P You would think the difference would be linear ..
Perhaps we should throw some ezGraphing at it, see what happens ..
Thomas Koch •
I measured the time too. It's nearly the same with a slight advantage vor Arrays.Arrays: Memory: 741280 Time: 0.03552485
Objects: Memory: 712176 Time: 0.03919482
Evert •
Nice! Now there's pretty much no reason anymore to use arrays for stuff like thisprocedury •
Interesting result. Thanks!Amit Shah •
Nice and interesting result.We can also take full advantage of OOP using object.
Sebastian •
Hello,We have the same type of decision to make, but still we are thinking towards arrays.
The value objects will bring the power of OOP, but will remove the power of PHP, which are arrays.
I'm still amaized by all the powerfull functions that php comes with, when you have to manipulate arrays.
So how did you guys addressed this issue?
Evert •
Hey Sebastian,In most cases you're working with 2-dimensional arrays, we clearly keep the top-level an array (our rows) and each row is a class.
You'll find that most of those array functions you'll use, you only use it on the top-level array, and a lot less on the individual records, but maybe you have a real-life use-case?
James •
I am making a AJAX System using something like a Model View Controller (MVC) Design. I am using Arrays to hold each command. I think they are very flexible as they need to be different for each manager class they are sent to requires requires different data. I could create a Command class and create a object from that every time a AJAX request is made. But why would I do this?Biber hapı •
Good on your log