2 ms·
C# does indeed produce a rather tight assembly https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzmAPgAgLACgACATAMx4DeemVm+xNAjAGyYA2A9jAOaYDKArmAAU7LgH4A2gF1MCABaxOKA
by alkonaut 4y ago
C# does indeed produce a rather tight assembly
https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzmAPgAgLACgACATAMx4DeemVm+xNAjAGyYA2A9jAOaYDKArmAAU7LgH4A2gF1MCABaxOKAJQ5c1TBTXrqI7igGYAvJgAMAbkraqAMzYAnQbASYoR02ZcAeOQpQA6ABkAUy45DygAagilSyt1fTBMCOMfLhRxKEk/AAlUADUIFj4gzFFTTBAZeTSMrIKioIstK3wAdkwEpvUAXzxe3CA=== https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzmAPgAgLACgACATAMx...
- orthoxerox 4y agoThe compiler takes care of this, but the idiomatic way to write the body of the loop in C# is sum += things[i] ?? 0;
- alkonaut 4y agoYou could even argue the most idiomatic C# is things.Sum(x => x ?? 0) and there the real test would be if the compiler/jit would match the speed of the for() loop exactly, i.e. not allocating any enumerators on the heap, not boxing any values and so on.
- evntdrvn 4y agounfortunately LINQ-to-objects doesn't have the greatest reputation for generating great code/perf, due to a number of factors (largely due to historical impl/interface decisions, from what I understand)