[development] performance foo

Larry Garfield larry at garfieldtech.com
Wed Dec 14 06:01:29 UTC 2005


OK, I just checked and I was wrong.  I was thinking of array2object(), which 
was discussed here:

http://lists.drupal.org/archives/development/2005-09/msg01015.html

I'm not sure if there are edge cases that would affect object2array as well as 
array2object.  If not, then yay for performance improvements.  

*crawls back under his rock*

On Tuesday 13 December 2005 11:47 pm, Larry Garfield wrote:
> I seem to recall someone suggesting this before, and the problem was that
> the casting method didn't scale to nested objects and arrays.  It should be
> in the issue archive, somewhere. :-)
>
> On Tuesday 13 December 2005 09:53 pm, 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.
> >
> >
> > Here are the tested functions:
> >
> > function object2array($object) {
> >    if (is_object($object)) {
> >      foreach ($object as $key => $value) {
> >        $array[$key] = $value;
> >      }
> >    }
> >    else {
> >      $array = $object;
> >    }
> >    return $array;
> > }
> >
> > function better($object) {
> >      if (is_object($object)) {
> >          $array = (array) $object;
> >      }
> >      else {
> >          $array = $object;
> >      }
> >      return $array;
> > }
> >
> >
> > Worth a patch?
> >
> >
> > ..chrisxj

-- 
Larry Garfield			AIM: LOLG42
larry at garfieldtech.com		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson


More information about the development mailing list