23

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 ?

Jonny
  • 2,787
  • 10
  • 40
  • 62

1 Answers1

49

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:

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I would also add that link to your answer: http://msmvps.com/blogs/jon_skeet/archive/2011/01/01/reimplementing-linq-to-objects-part-20-tolist.aspx – MarcinJuraszek Feb 23 '13 at 15:20
  • @JonSkeet: wow, I didn't notice it's your blog post out there :) Have to say, really nice one! – MarcinJuraszek Feb 23 '13 at 15:23
  • @MarcinJuraszek Yeah, check out the full [Edulinq series](http://msmvps.com/blogs/jon_skeet/archive/tags/Edulinq/default.aspx) and the ebook. Fabulous stuff and really helps to understand Linq, collection iteration, and just some of the elegance of it in general; really expanded my thinking. Speaking of the ebook, any plans Jon on taking a second pass updating/editing the published PDF version? I would totally buy a fully polished and printed version of it. :) – Chris Sinclair Feb 23 '13 at 15:29
  • @ChrisSinclair: There's a publisher who actually has the rights to it, but I still don't know if they're planning on using them... – Jon Skeet Feb 23 '13 at 16:39
  • @JonSkeet, the blog links are now broken. Looks like the first segment of the path changed from `jon_skeet` to `jonskeet`. Here are the new links, in case anyone needs them: [ToList](http://blogs.msmvps.com/jonskeet/2011/01/01/reimplementing-linq-to-objects-part-20-tolist/), [ToArray](http://blogs.msmvps.com/jonskeet/2011/01/02/reimplementing-linq-to-objects-part-24-toarray/), [ToDictionary](http://blogs.msmvps.com/jonskeet/2011/01/02/reimplementing-linq-to-objects-todictionary/) – rdumont Oct 16 '14 at 21:09
  • @rdumont: That's still my old blog - I've updated the links to point to my new blog. – Jon Skeet Oct 16 '14 at 21:20