[TYPO3-dev] FYI: comparing execution speed of some php functions

Wiel, J.A.M. van de j.a.m.v.d.wiel at tue.nl
Fri Jun 26 18:37:27 CEST 2009


Hi Rupert,
Great work!! I learned some new things today, thanks! 

I've been aware of the speed advantage of foreach for some time now. Apart from the speed, it's also much cleaner code-wise. 

The result of your instantiated object vs. static call test makes sense to me if, during instantiation, PHP does some work on the object's methods which it doesn't do for uninstantiated classes. In that case I'd be interested in how much time it takes to actually instantiate an object and then call a method, versus calling a method statically. If you only need a single shot you should probly call static but in any kind of loop it could be wise to instantiate first, do your loop magic, and clean up after yourself if appropriate by destroying the object again. If I'm making any kind of sense here at all (it's too hot to think straight here), I'll be reconsidering the way I use t3lib_div in my projects.

I'll take some time this weekend to run a similar test on a system with eAccelerator and without it. It might just mitigate any difference between static calls and instantiated objects after the first pass..

What do you think?

Bas

PS. On a related note: minimizing DB access like you did recently is probably a bigger source of performance gains. DB calls are incredibly expensive, relatively speaking.
________________________________________
From: typo3-dev-bounces at lists.netfielders.de [typo3-dev-bounces at lists.netfielders.de] On Behalf Of Rupert Germann [rupi at gmx.li]
Sent: Friday, June 26, 2009 5:20 PM
To: typo3-dev at lists.netfielders.de
Subject: [TYPO3-dev]  FYI: comparing execution speed of some php functions

hi devs,

I've read a lot of articles about performance in webapplications during the
last days and stumbled (again) upon questions like:
is is_array() faster than count()? or how much faster is foreach() compared
to while(list()...)?
and many more.

to get some reliable results I wrote a little test script which you can
download here http://rgdata.de/fileadmin/typo3_dev/speed_test.php.txt

The script iterates through a big array with 100000 entries and measures the
execution time of certain operations.

here are some of the results:

for() loop:
---------------------------------------------
for() loop using count() took           20.86401 ms
for() loop Not using count() took       7.09796 ms

using count() means:
for ($i = 1; $i < count($myarr); ++ $i) {...

OK, I think that should not be a new information for anyone here ;-)
but what about while and foreach?

foreach() loop:
---------------------------------------------
foreach() took                  11.16920 ms
foreach() with KEY took         12.35318 ms

while(list()=each()) loop:
---------------------------------------------
while(list()...) took           58.12597 ms
while(list()...) with key took  64.06283 ms


interesting, isn't it?
That means even if I have something like this in my code:

while(list($key) = each($arr)) {
  // do smthg that needs only $key
}

it will be way slower than:

foreach($arr as $key => $val) {
  // do smthg that needs only $key
}

although $val is never used.

If I count the occurences of "while(list(" in class tslib_content I see some
potential here.

(FYI: the patch for class tslib_content is almost ready)

another interesting question: are statically called methods faster or those
from instantiated objects?

calling methods in classes:
---------------------------------------------
object call took                40.79103 ms
derived object call took        40.16995 ms
static call took                102.13280 ms

hmm, until now I thought it is exactly the other way round. Could someone
plaese have a look to my testscript and tell me if this behaviour is caused
by my testing method. strange.


next test: accessing array vars

let's say we have a piece of code like this:
function arraytest($num) {
        for ($i = 0; $i < $num; ++ $i) {
                if ($GLOBALS['foo']['bar'] == "test") {
                        $j = 1;
                } else {
                        $j = FALSE;
                }
        }
}

quite common in TYPO3 source - e.g. all those checks involving
$GLOBALS['TYPO3_CONF_VARS']...

if we now change the code like below it will run faster:

function arraytest2($num) {
        $testvar = $GLOBALS['foo']['bar'];
        for ($i = 0; $i < $num; ++ $i) {
                if ($testvar == "test") {
                        $j = 1;
                } else {
                        $j = FALSE;
                }
        }
}

results:
---------------------------------------------
accessing array took                    21.87395 ms
accessing local var took                13.07201 ms


the script speed_test.php makes some more tests e.g. measuring the speed of
different string comparing methods.

If you know some commonly used code constructs that can be substituted by
faster variants - let me know!

happy benchmarking!

greets
rupert


further reading:
http://www.tuxradar.com/practicalphp/18/0/0
http://phplens.com/lens/php-book/optimizing-debugging-php.php



_______________________________________________
TYPO3-dev mailing list
TYPO3-dev at lists.netfielders.de
http://lists.netfielders.de/cgi-bin/mailman/listinfo/typo3-dev




More information about the TYPO3-dev mailing list