Comments
Ahh that was stupid =P I reset the array after the second xdebug_memory_usage( );
I'll update this now
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;
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
Arnold,
You're right .. I was just hoping since PHP knows about those properties, it would handle the defined properties slightly different.
Evert
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 :)
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
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.
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 ..
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
Nice! Now there's pretty much no reason anymore to use arrays for stuff like this
Nice and interesting result.
We can also take full advantage of OOP using object.
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?
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?
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?







