[Phpug] Performance: Static versus Intance Methods

David Coallier davidc at php.net
Fri Apr 11 03:51:44 IST 2008


On 10/04/2008, Michał Słaby <michal.slaby at epsi.pl> wrote:
> On Wed, 2008-04-09 at 20:23 -0400, kprichard wrote:
>  > I'm jumping into this thread a bit late, but thought the conversation
>  > was worth following up on.  You say,
>  >
>  > > In fact, objects in PHP are blazing
>  > > fast and fairy lightweight. Objects are so fast, that Zend Engine
>  > > developers advise to wrap arrays in objects to gain speed:
>  > >
>
> > This made me very curious - there's been an ongoing debate about
>  > obtaining best PHP performance at my workplace.  So, I copied your two
>  > foreach() constructs and extended them to perform four tests:
>  >
>  > 1) iterate simple array with for(),
>  > 2) iterate simple array with foreach(),
>  > 3) iterate an ArrayObject with foreach(),
>  > 4) iterate an ArrayObject with the built-in iterator methods.
>  >
>  > Script below, here are results produced on a Solaris x86 box w/PHP
>  > 5.2.0/Zend v2.2.0, running from CLI-
>  >
>  > 1. 0.0024580955505371
>  > 2. 0.0039119720458984
>  > 3. 1.1935360431671
>  > 4. 1.2200510501862
>  >
>  > These ratios are pretty consistent, test after test, though perhaps
>  > splitting the script into four separate files would be a fairer test.
>
>
> Indeed, I ran your test on Linux 2.6.24-x86 and PHP 5.2.4 and got:
>  1. 0.00915884971619
>  2. 0.00682306289673
>  3. 2.44940495491
>  4. 2.58817601204
>
>  This is really surprising to me, so I made simplified test:
>  $a1 = array();
>  $a2 = new ArrayObject();
>
>  for ($i = 0; $i < 1000; ++$i) {
>         $a1[] = 1;
>         $a2[] = 1;
>  }
>
>  $t = microtime(true);
>  foreach ($a1 as $item) ;
>  echo (microtime(true) - $t) . "\n";
>
>  $t = microtime(true);
>  foreach ($a2 as $item) ;
>  echo (microtime(true) - $t) . "\n";
>
>  The results were:
>  0.0125710964203
>  0.0390491485596
>

Actually I am sorry to be the party breaker, however, php 5_3 (which
is still CVS) is faster with ArrayObjects than normal arrays. This is
due to the opcode optimizations that have been done.

On the other hand, in php 5.2.x versions, arrays are faster until you
start using APC and opcode caches.

The main point for SPL usage is definitely the maintenance and
standardization of the code. But that is an whole other issue. One way
to make this equally faster.

On another note, if we are to test the functionalities of ArrayObject,
please let's take in factor all what it does imply. For instance, the
offsetGet and offsetExists can let you find the value and get it
easily, also that it uses a whole other set of functionalities like
extending to IteratorAggregate   , Traversable   , ArrayAccess  and
Countable. All of those will give you a whole other range of
opportunities which will end up being much  faster than trying to do
all sorts of array handling.

Also, when appending values to an array object, let's use
$obj->append($val); instead of $obj[] = $val ;-) That's the native
way.

I will personally investigate why this is slower (which I think I
already know the answer to) and try to fix it in cvs (php's cvs).

So let's make it clear :P The basic functionalities are faster with
native arrays than ArrayObject, however, when using extended
functionalities of arrays, ArrayObject is much faster.

Hmm, I hope this clears a bit of questions, I'll keep you posted on
the progress of the investigation.

Thanks,
David


More information about the Phpug mailing list