News for hackers. Stuff that matters.
Re:.NET 6 vs .NET 5 speedup by foepys on Sunday November 21, 2021 @03:54PM (#29299614)
> - Use LINQ cautiously as its variants are mostly slower than explicit coding. E.g. .Any() vs .Count == 0

When using LINQ also be aware that .First(predicate) is significantly slower than .Where(predicate).First() when called on List<T> and T[]. This is true for essentially all methods like Last, Single, Count etc. Don't trust Visual Studio when it's telling you to "optimize" this.

But if you want the last bit of performance, you shouldn't use LINQ anyways.

Re:.NET 6 vs .NET 5 speedup by joe_guy on Sunday November 21, 2021 @05:14PM (#29300284)
Do you know why that is? That's very interesting.
Re:.NET 6 vs .NET 5 speedup by sbelskie on Sunday November 21, 2021 @07:54PM (#29301438)
Not sure if it is still the case, but it used to be that First did a fairly naive foreach over the IEnumerable while Where has several collection specific type checks that allow it to use MoveNext and maybe other more efficient ways to traverse the collection.
Re:.NET 6 vs .NET 5 speedup by foepys on Monday November 22, 2021 @12:46AM (#29303206)
LINQ is parsing a tree of System.Linq.Expression here and the cases of First(pred) etc. are just not optimized because of the added complexity with little benefit. It only recently became a problem when Visual Studio got a new built-in analyzer that tells people to "optimize" this.
Re:.NET 6 vs .NET 5 speedup by sobani on Monday November 22, 2021 @07:43AM (#29305757)
If you're using LINQ on a list, it will not use Expressions, but a plain Func. So nothing will get parsed either way.

The difference, as @sbelskie already mentioned, is that Where has an optimization for List, while First only uses the naive enumerator.