3 ms·
Are they? LINQ is usually slower than for loop
by high_na_euv 2y ago
Are they? LINQ is usually slower than for loop
- pjc50 2y agoQuite often it compiles to the same IL. Would you like to provide some godbolt examples where it's significantly different?
- neonsunset 2y agoIL is always different since it's a high-level bytecode. The machine code output is different too. Now, with guarded devirtualization of lambdas, in many instances LINQ gets close to open-coded loops in performance, and it is very clever in selecting optimal internal iterator implementations, bypassing deep iterator nesting, having fast-paths for popular scenarios, etc. to achieve very good performance that can even outperform loops that are more naive, but unfortunately we're not there yet in terms of not having a baseline overhead like iterators in Rust. There is a selection of community libraries that achieve something close to this, however. I would say, LINQ performance today gets as close as it can to "it's no longer something to be worried about" for the vast majority of codebases.
- high_na_euv 2y agoTwo or so years ive been developing library and i remember where switching from something simple like First or FirstOrDefault to for loop made difference when using benchmark dotnet Then I found that it was common knowledge that linq is slower, even among ppl on c#s discord