Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Сложность алгоритма в LINQ



MembershipUserCollection membershipUsers = new MembershipUserCollection();
users.ToList().ForEach(membershipUsers.Add); //what is the complexity of this line?
http://stackoverflow.com/questions/4008682/what-is-the-complexity-of-this-linq-example?rq=1


There are very, very few guarantees, but there are a few optimizations:
  • Extension methods that use indexed access, such as ElementAtSkipLast or LastOrDefault, will check to see whether or not the underlying type implements IList<T>, so that you get O(1) access instead of O(N).
  • The Count method checks for an ICollection implementation, so that this operation is O(1) instead of O(N).
  • DistinctGroupBy Join, and I believe also the set-aggregation methods (UnionIntersect and Except) use hashing, so they should be close to O(N) instead of O(N²).
  • Contains checks for an ICollection implementation, so it may be O(1) if the underlying collection is also O(1), such as a HashSet<T>, but this is depends on the actual data structure and is not guaranteed. Hash sets override the Contains method, that's why they are O(1).
  • OrderBy methods use a stable quicksort, so they're O(N log N) average case.
http://stackoverflow.com/questions/2799427/what-guarantees-are-there-on-the-run-time-complexity-big-o-of-linq-methods

101 Пример использования LINQ

Нашел ссылку, очень много полезных примеров применения LINQ http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b