Here's an enumeration of random integers:
var r = new Random();
var e = Enumerable.Repeat(1, 10_000_000).Select( _ => r.Next());
Which version do you think is faster:
var result = e.OrderBy(x => x).Last(); //materialize the IEnumerable
or
var l = e.ToList();
l.Sort();
var result = l.Last();
I was hoping that .OrderBy(x => x).Last()
in the first example would be
optimized to only sort a small subset of the list or to just do an O(n) walk of the list.
Spoiler: It's not.
But then, the performance of the two versions should be at least comparable, right? I mean:
In the first one, OrderBy()
allocates a temp array and sorts it in place.
In the second one, I explicitly allocate a list and Sort()
it in place.
The actual results show that the OrderBy()
example is 4-5x slower! (5-6 sec vs 1.2-1.3 sec)
Can anyone come up with an explanation why?
The .OrderBy(x => x)
case does execute its x => x
identity lambda for each element.
The difference between:
var result2 = e.Last();
and
var result2 = e.Select(x=>x).Last();
is measurable, but small: 30 - 50 ms more in the second case. So that doesn't explain the huge gap.