Welcome Guest, Not a member yet? Register   Sign In
Database results as an array or object? - benchmarks included
#1

[eluser]Xeoncross[/eluser]
I am a big performance buff - I love to squeeze everything I can out of my code. But at the same time it is just stupid do something the wrong way just to get a tiny-tiny bit of extra speed. ($time = 60 * 60 * 24 vs $time = 86400).

So now that you know where I am coming from - I want to pick your brains on the whole array vs object results thing (and I have some data).

I ran some benchmarks on object/array creation and storage and came up with some numbers. Please not that this is NOT a benchmark from the CI functions result() and result_array() as too many other factors would affect a benchmark of this size. Instead I just use plain array() and stdClass() to create the results.

Code:
//Numbers
//100,000 numbers from 0 to 100,000

ARRAY : 7543.4 kbytes Used
0.074545 Time

OBJECT: 8184.6 kbytes Used
0.162911 Time

//Strings
//10,000 [a-z] values from 5 - 40 chars long (same array used for both)

ARRAY : 491.7 kbytes Used
0.006808 Time    

OBJECT: 491.6 kbytes Used
0.013479 Time

As you can see only with large result sets would the 2x gain that arrays have over objects become important. So with this known, is there some reason arrays might be evil when used as result sets - after all it isn't like OOP applies as the result isn't going to be extended or set as a object to be inherited.

I used to use arrays - then I moved to PDO (and later CI) and loved the object results - but now I am questioning it.

Any thoughts? (Other than "it's too small to mater"...)
#2

[eluser]Phil Sturgeon[/eluser]
Never really known any noticeable difference between the two. As you said we aren't going to be doing anything OOPy with the object so whether its an array or an object, the only difference for me is the syntax.

It's interesting to know arrays are actually x2 on the speed (that settles an argument from about 2 years ago, if only I could remember who I was having it with...) but I don't think I'll be recoding any of my sites jjust for this.
#3

[eluser]m4rw3r[/eluser]
Another fact (I don't have benchmarks, but if anyone requests, I will do some) is that fetching an array and then manually populating a class with the resulting data is faster than mysql_fetch_object($result_id, 'My_Class').




Theme © iAndrew 2016 - Forum software by © MyBB