DEV Community

Discussion on: A Slow Death by For Loops

Collapse
 
bengreenberg profile image
Ben Greenberg

Yes, I agree there are some great tools out there for managing multiple sources of data, but sometimes we are limited by the business specifications. I also agree that foreach() looks a lot nicer than a for() loop, but I'm not 100% sure that it is faster than a for() loop with the length calculated in advance. I've seen a lot written about that subject with various benchmarks going both ways. Of course, in most situations, the difference will be negligible either way.

Collapse
 
vasilvestre profile image
Valentin Silvestre

As you can see here : 3v4l.org/7Kvf5 , PHP7 made foreach() 50% as fast as for(). It depend of version tho
To be honest, that's premature optimisation to use for() instead of foreach() from now. If you really need high-performance, you shouldn't use PHP imo

Thread Thread
 
yechielk profile image
Yechiel Kalmenson

Unfortunately, sometimes those decisions are made for you by others...

Thread Thread
 
vasilvestre profile image
Valentin Silvestre

Sometimes it's worth mentionning the fact that with low effort, you can improve a lot the process and (because that's the only thing that interest people) the result