3 ms·
TL;DR: Yes & use foreach rather than for to skip array bound checks.
by potiuper 7y ago
TL;DR: Yes & use foreach rather than for to skip array bound checks.
- deleted 7y ago[deleted]
- vardump 7y agoYeah. A slight oops from CLR optimizer, but nothing serious. It seemed to miss a hoisting optimization opportunity in the foreach case.
- Gibbon1 7y agoLong 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.
- deleted 7y ago[deleted]
- yc12340 7y agoIt 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.
- gameswithgo 7y agoc# does not need a foreach loop for bound check elimination.
- gameswithgo 7y agoit 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.