[development] performance foo

Chris Johnson chris at tinpixel.com
Wed Dec 14 17:49:54 UTC 2005


Original results, with current Drupal object2array():
>> 10000 'object2array' calls took 3.04 seconds.
>> The average was 0.00030442 seconds per call.

With object2array() rewritten to remove loop and just cast the object to an 
array (i.e. $array = (array) $object, but otherwise unchanged function:
>> 10000 'better' calls took 1.27 seconds.
>> The average was 0.00012693 seconds per call.

Neil Drumm wrote:
> Test out 10k calls to get_object_vars().

Because he has never seen object2array() called with anything but an object.

Interesting idea.  So I rewrote object2array() and better() like this:

function object2array($object) {
     return get_object_vars($object);
}

function better($object) {
     return (array) $object;
}

And here are the results:

10000 'object2array' calls took 1.14 seconds.
The average was 0.00011444 seconds per call.

10000 'better' calls took 1.05 seconds.
The average was 0.00010478 seconds per call.


So get_object_vars() is definitely faster than what we have now, but casting 
appears to be slightly faster still.  Probably saving an interpreted function 
call and value return is the whole savings, as the internal C code for 
get_object_vars() and the cast are probably one and the same.

Best of all, if someone does pass an array, casting an array to an array is a 
no-op, and the function still works the same.

I will roll a patch today.

..chrisxj



More information about the development mailing list