4 ms·
Nice benchmarking on Microsoft .Net CLR. Looks like the JIT compiler is smart enough to recognize that array.Length is an invariant and hoist it out of the loop
by 60654 7y ago
Nice benchmarking on Microsoft .Net CLR. Looks like the JIT compiler is smart enough to recognize that array.Length is an invariant and hoist it out of the loop, which is awesome for the common use cases!
One nitpick about the title: C# runs on more runtimes than Microsoft .Net CLR, and those may behave very differently. For example: Mono CLR, or Unity's IL2CPP which is an ahead-of-time compiler.
Specifically, I'd expect IL2CPP would not hoist length out, because it would not recognize it as an invariant. (Some great examples of IL2CPP cross compilation are here: https://jacksondunstan.com/articles/4749 https://jacksondunstan.com/articles/4749 )
TLDR: the Microsoft JIT compiler makes the local variable unnecessary, but this is a property of the JIT, not of C#. Developers on non-MS platforms shouldn't assume this.
- chrisseaton 7y ago> those may behave very differently They shouldn’t behave any differently should they? There’s a single language spec.
- tomsmeding 7y ago"Behave very differently" is meant with regards to performance, here; or in other words, things that are not observable in the language semantics.
- pjmlp 7y agoJava's language spec also doesn't dictate how escape analysis or remapping from classes into structs gets done, for example.