Hacker News new | past | comments | ask | show | jobs | submit login

TL;DR: Yes & use foreach rather than for to skip array bound checks.



it will skip the array bounds check as long as myArray.Length is the terminal condition in the for loop, rather than a local variable with the length stored.


It is odd, that C# needs foreach loop for bound check elimination.

Java supports this optimization for a wide range of loop types... since Java 7, I think. Normally I would argue, that Java is just ahead of curve, but Android has also gained supports for bounds check elimination in 2014-2015.

Either article does not tell us whole truth or Microsoft JIT is subpar by modern standards.


c# does not need a foreach loop for bound check elimination.


Yeah. A slight oops from CLR optimizer, but nothing serious. It seemed to miss a hoisting optimization opportunity in the foreach case.


Long time ago I decided that you shouldn't second guess the compiler without a reason. So just avoid worrying about local optimizations and focus on your data structures and algorithms.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: