[Phpug] Performance: Static versus Intance Methods
Michał Słaby
michal.slaby at epsi.pl
Fri Apr 11 00:08:32 IST 2008
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
It is still slower, but by a more reasonable factor.
I based my previous posting on advice given by Adam Trachtenberg in book
Upgrading to PHP5 - http://www.oreilly.com/catalog/upgradephp5/
He claims that iteration over ArrayObject is faster than old fashion
arrays.
However, numbers don't lie, so let's agree that array()s are still
faster.
Regards,
Michael
More information about the Phpug
mailing list