LINQ的了ToList /的ToArray / ToDictionary性能 [英] Linq ToList/ToArray/ToDictionary performance

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

问题描述

嗯,我遇到的许多情况下,有一个IEnumerable是不够的。但是我不能确定的上述方法调用的性能。

我真正想问的是:

时了ToList /的ToArray的表现:

  1. 在一个O(n)的操作,它复制了IEnumerable到一个新的数组/列表?
  2. 如果我叫了LINQ延伸方法的名单上,它有一个O(1)性能,如果我叫了ToList但为O(n),如果调用的ToArray(如果我原来的名单是一个数组相反)?

  3. 有些神奇发生,表现为O(1)?

大概意思是O(n),对吧?

解决方案
  

了ToList / 的ToArray 为O(n)操作的性能,它复制了的IEnumerable 来一个新的数组/列表?

是的。 了ToList 会更有效,因为它并不需要修剪内部缓冲器到合适的长度第一。

  

如果我打电话给名单上的LINQ延伸的方法,它有一个O(1)性能,如果我叫了ToList但为O(n),如果调用的ToArray(如果我原来的名单是一个数组相反)?

没有。对于这两个电话,一个的的集合始终创建;这就是原始集合的浅表副本。这是更有效地调用了ToList 的ToArray 任何的ICollection< T> 不是一个简单的的IEnumerable< T> 不落实的ICollection< T> 不过,与集合中的长度是已知的开始。 (这是通过检测执行时,虽然,你并不需要担心编译时类型)

  

大概意思是O(n),对吧?

假设哈希是明智的,这是O(N),是的。基本上,它正是你可能希望它的方式创建一个新的字典。

您可能需要阅读相应的职位,我Edulinq博客系列:

  • <一个href="http://$c$cblog.jonskeet.uk/2011/01/01/reimplementing-linq-to-objects-part-20-tolist.aspx"><$c$c>ToList
  • <一个href="http://$c$cblog.jonskeet.uk/2011/01/02/reimplementing-linq-to-objects-part-24-toarray.aspx"><$c$c>ToArray
  • <一个href="http://$c$cblog.jonskeet.uk/2011/01/02/reimplementing-linq-to-objects-todictionary.aspx"><$c$c>ToDictionary

Well I encounter many situations where having an IEnumerable is not enough. However I'm unsure about the performance of the above method calls.

What I really want to ask is:

Is the performance of ToList/ToArray:

  1. an O(n) operation which copies the IEnumerable to a new array/List ?
  2. If I called a linq extention method on a list, it has an O(1) performance if I call ToList but O(n) if call ToArray (and the opposite if my original list was an array) ?

  3. Some magic happens and the performance is O(1)?

Probably to Dictionary is O(n), right ?

解决方案

Is the performance of ToList/ToArray an O(n) operation which copies the IEnumerable to a new array/List ?

Yes. ToList is slightly more efficient, as it doesn't need to trim the internal buffer to the right length first.

If I called a linq extention method on a list, it has an O(1) performance if I call ToList but O(n) if call ToArray (and the opposite if my original list was an array) ?

No. For both calls, a new collection is always created; that's a shallow copy of the original collection. It's more efficient to call ToList or ToArray on any ICollection<T> than on a simple IEnumerable<T> which doesn't implement ICollection<T> though, as with a collection the length is known to start with. (This is detected at execution time though; you don't need to worry about the compile-time type.)

Probably to Dictionary is O(n), right ?

Assuming the hash is sensible, it's O(N), yes. Basically it creates a new dictionary in exactly the way you'd probably expect it to.

You might want to read the corresponding posts in my Edulinq blog series:

这篇关于LINQ的了ToList /的ToArray / ToDictionary性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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