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

Niels Pardon mail at niels-pardon.de
Fri Jun 26 17:27:48 CEST 2009


Hi Rupert!

Thanks a lot for sharing your work! That's really interesting stuff
every PHP-developer can use.

Greets,

Niels



Rupert Germann schrieb:
> 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
> 
> 
> 




More information about the TYPO3-dev mailing list