[TYPO3-core] RFC: #13670: Performance optimization: change while(list() to foreach()

Ernesto Baschny [cron IT] ernst at cron-it.de
Wed May 19 23:06:12 CEST 2010


Susanne Moog schrieb am 19.05.2010 20:45:

>> This is an SVN patch request.
>>
>> Type: Bugfix, Performance tuning
>>
>> Bugtracker references:
>> http://bugs.typo3.org/view.php?id=13670
>>
>> Branches: trunk, 4.3
>>
>> Problem:
>> foreach($arr as $key=>$value) is faster than while (list($key,
>> $value)=each($arr)) so it should be changed
> 
> I know I'm a bit late, but I wanted to ask if there's any proof /
> statistics that this really tunes performance as I just happened to look
> at http://www.phpbench.com/ which seems to suggest otherwise if I read
> it correctly (see section: foreach() vs. for vs. while(list() = each())).
> 
> Sorry for asking this ;)

The test you are refering to is a "modify loop". If you scan a bit down
on that page there is also the "read loop", which only reads the values
and proves that "foreach" is about 6x faster.

I haven't checked if all our accesses are read-only, but I am pretty
confident that most of them are, which makes the change is justified.
For the ones that aren't I think it makes sense to keep it "foreach" to
keep it consistent readable.

We could agree to keep while/each on modify-loops which are heavily
used. To spot those we need more benchmarking.

Cheers,
Ernesto


More information about the TYPO3-team-core mailing list