简体   繁体   English

LINQ orderby vs IComparer

[英]LINQ orderby vs IComparer

I would like to know what is better to use. 我想知道什么是更好用。

IComparer class and Compare method for sort or LINQ orderby on List. 用于排序的IComparer类和比较方法或列表上的LINQ orderby。 Both works fine but which one is better for large lists. 两者都工作正常但哪一个更适合大型列表。

I would choose LINQ for two reasons. 我会选择LINQ有两个原因。

  • LINQ queries are generally shorter and easier to read. LINQ查询通常更短且更易于阅读。
  • If you really do have a large number of elements, Linq also gives you the ability to scale out to multiple CPU cores by using PLinq, which might help you out significantly. 如果您确实拥有大量元素,Linq还可以使用PLinq 扩展到多个CPU核心 ,这可能会对您有很大帮助。

I would expect performance to be roughly similar for a single-threaded implementation, if you consider that the lambda expression in your OrderBy clause compiles to a function -- which is pretty much all you get by implementing IComparer anyway. 如果你认为OrderBy子句中的lambda表达式编译成一个函数,我希望单线程实现的性能大致相似 - 这几乎是你实现IComparer所能得到的。

That being said, you might get more of a performance boost by changing your sort algorithm to be tailored to how your data is already sorted, rather than by changing your comparison method. 话虽如此,您可以通过更改排序算法来获得更多性能提升,以便根据数据的排序方式进行定制,而不是通过更改比较方法。 But I'd be willing to bet my coffee this morning that OrderBy in your Linq statements uses an implementation of Quicksort, so it's probably pretty decent in the general case already. 但是我愿意今天早上打赌我的咖啡,你的Linq语句中的OrderBy使用了Quicksort的实现,所以在一般情况下它可能已经相当不错了。

I prefer using LINQ by default for all collection-based operations. 我更喜欢在所有基于集合的操作中使用LINQ。 The advantage here is that I don't have to assume too much on the type of the collection used (OrderBy works on IEnumerable). 这里的优点是我不必过多地假设所使用的集合的类型(OrderBy适用于IEnumerable)。

If you have an IList<T> anyway, then List.Sort will probably be faster. 如果你有一个IList<T> ,那么List.Sort可能会更快。

Anyhow, I wouldn't worry about it before there is a proven (ie measured) performance problem 无论如何,在有经证实(即测量)的性能问题之前,我不会担心它

I think semantically the two are very different, IComparer interface lets you define how your type is sorted naturally, OrderBy gives you a way to sort your objects by some specific key, eg given a list of Person objects, for query A sort the list by First Name, for query B sort the list by Age. 我认为在语义上两者是非常不同的,IComparer接口允许您自然地定义类型的排序方式,OrderBy为您提供了一种通过某些特定键对对象进行排序的方法,例如,给定一个Person对象列表,用于查询A按列表排序名字的名字,用于按年龄对列表进行排序。

LINQ gives you more flexibility, but as OrderBy requires a Func which takes your object type and returns a key to use for sorting, whatever key you return will still need to implement the IComparer interface. LINQ为您提供了更大的灵活性,但由于OrderBy需要一个Func,它接受您的对象类型并返回一个用于排序的键,无论您返回什么键,仍然需要实现IComparer接口。

In terms of performance on a large list, depending on what you're doing in the Compare method there are probably very little difference between the two approaches I'd imagine, though it's best to just test it out against your type. 就大型列表的性能而言,取决于你在Compare方法中所做的事情,我想象的两种方法之间可能差别很小,尽管最好只针对你的类型进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM