[development] performance foo

Neil Drumm drumm at delocalizedham.com
Wed Dec 14 06:36:30 UTC 2005

Chris Johnson wrote:
> Just came across something that triggered a bit of performance testing.  
> One can cast objects to arrays and vice-versa in PHP.
> So, in common.inc, we have a function called object2array() that loops 
> through  an object and makes it into an array.  Is that faster than the 
> equivalent function using a cast (array) operator?  No.  In fact, it's 
> about 2.4 times slower.  However, both are very fast for an object with 
> 26 elements.  Here's the benchmark:
> 10000 'object2array' calls took 3.04 seconds.
> The average was 0.00030442 seconds per call.
> 10000 'better' calls took 1.27 seconds.
> The average was 0.00012693 seconds per call.

Test out 10k calls to get_object_vars().

I've been running Drupal for a few months with a version of 
object2array() to see if the array handling is ever used:

function object2array($object) {
   if (is_object($object)) {
     foreach ($object as $key => $value) {
       $array[$key] = $value;
   else {
     trigger_error('Watch yer type!!');
     $array = $object;

   return $array;

I haven't run into object2array being called with anything that isn't an 
object yet.

Neil Drumm

More information about the development mailing list