Why is List<>.OrderBy LINQ faster than IComparable+List<>.Sort in Debug mode?

前端 未结 4 2169
情歌与酒
情歌与酒 2021-02-19 11:30

I was interested in whether it would be faster to sort my classes using LINQ, or by implementing the IComparable interface and List.Sort. I was quite surprised when the LINQ cod

4条回答
  •  旧巷少年郎
    2021-02-19 12:09

    If you make sure everything is JITed before starting the measure, you might get different results (I also recommend using the Stopwatch class to measure time):

    var ll = ts2.ToList();
    ll.Sort();
    ll.OrderBy(item => item.Age).ToList();
    

    According to my measurements (after adding the above code), IComparable is always faster (even in debug).

提交回复
热议问题