LINQ的OrderBy VS的IComparer [英] LINQ orderby vs IComparer

查看:358
本文介绍了LINQ的OrderBy VS的IComparer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是更好的使用。

I would like to know what is better to use.

的IComparer类和对列表进行排序或LINQ的OrderBy比较方法。这两个工作正常,但哪一个是大型列表更好。

IComparer class and Compare method for sort or LINQ orderby on List. Both works fine but which one is better for large lists.

推荐答案

我会选择LINQ的原因有两个。

I would choose LINQ for two reasons.


  • LINQ查询一般都较短,更容易阅读。

  • 如果你确实有大量的元素,LINQ的也给你的能力的向外扩展通过使用PLINQ 的多个CPU内核,这可能显著帮助你。

  • LINQ queries are generally shorter and easier to read.
  • 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.

我期望的性能是单线程的实施大致相似,如果你认为你的排序依据子句中的lambda表达式编译为一个功能 - 这是几乎所有由你反正实施的IComparer获得

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.

话虽这么说,你可能会改变你的排序算法获得更多的性能提升来为您量身打造的数据是如何已经排序,而不是改变你的比较方法。但我愿意今天上午排序依据你的Linq语句使用快速排序的实现赌我的咖啡,所以它可能已经在一般情况下相当不错的。

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 VS的IComparer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆