[TYPO3-Performance] optimizing for loops

Dmitry Dulepov [typo3] dmitry at typo3.org
Thu Jul 24 10:48:46 CEST 2008


Hi!

Michiel Roos [netcreators] wrote:
> I was wondering about the 'var='.$var is faster than "var=$var" issue
> and came accross some optimization articles. One of them stated the
> example below (which I had never thought of). It appears that when using
> for loops the sizeof($arr) is evaluated multiple times.

:)

> So I grepped the code of the trunk and found a couple of lines like:
> 
> t3lib/class.t3lib_cs.php:1940:
>     for ($i=0; strlen($str{$i}) && $i<$len; $i++) {
> 
> t3lib/class.t3lib_cs.php:1995:
>     for ($i=0; strlen($str{$i}); $i++) {
> 
> t3lib/class.t3lib_cs.php:2032:
>     for ( ; strlen($str{$i}) && $n<$p; $i+=$d) {
> 
> t3lib/class.t3lib_tceforms.php:1342:
>       for ($c=0;$c<count($selItems);$c++) {
> 
> t3lib/class.t3lib_tceforms.php:1382:
>     for ($c=0;$c<count($selItems);$c++) {
> 
> t3lib/class.t3lib_tcemain.php:2024:
>          for ($a=strlen($value); $a>0; $a--) {
> 
> typo3/sysext/adodb/adodb/drivers/adodb-mssql.inc.php:538:
>    for ($i = 1, $max = sizeof($sqlarr); $i < $max; $i++) {

Nothing to add... Most of core code is not written with clarity and performance in mind :( This now changes but not as fast as wanted.

> So I think there is room for optimization in the core. I don't know what
> parts are the current performance bottlenecks. Maybe more speed
> improvements can be gained elsewhere. But since this was a new idea to
> me, it may help someone else.

I did some profiling a year ago or so. I think some changes were integrated. They speed up functions like loadTCA significantly (up to 10 times if I remember correctly). But it took long time to get this patch into core. Too long to spend time on more profiling :(

General problem with such changes is that sometimes they are too revolutionary. And they can be tested only if you use PHP profiler.

> Here is one simple example that prints an array:
> 
> for ($j=0; $j<sizeof($arr); $j++)
>     echo $arr[$j]."<br>";
> 
> This can be substantially speeded up by changing the code to:
> 
> for ($j=0, $max = sizeof($arr), $s = ''; $j<$max; $j++)
>   $s .= $arr[$j]."<br>";
> 
> echo $s;

But this may increase memory usage. There should be a good thinking in each case.

> An alternate way of speeding the above code would be to use output
> buffering. This will accumulate the output string internally, and send
> the output in one shot at the end of the script. This reduces networking
> overhead substantially at the cost of more memory and an increase in
> latency. In some of my code consisting entirely of echo statements,
> performance improvements of 15% have been observed.
> 
> ob_start();
> for ($j=0, $max = sizeof($arr), $s = ''; $j<$max; $j++)
>    echo $arr[$j]."<br>";

TYPO3 does this already.

-- 
Dmitry Dulepov
TYPO3 Core team
More about TYPO3: http://typo3bloke.net/
Subscribe: http://typo3bloke.net/rss.xml
Latest article: http://typo3bloke.net/post-details/extension_updates_loginusertrack_and_comments/


More information about the TYPO3-Performance mailing list