If I do the something like the following with LINQ:
void DoSomeStuffWithHashSet()
{
HashSet<int> set = new HashSet<int>();
for (int i = 0; i < 100; ++i) set.Add(i);
if (Lookup(set, new Random().NextInt(200))
System.Console.WriteLine("Yey");
else
System.Console.WriteLine("Ney");
}
bool Lookup(IEnumerable<int> haystack, int needle)
{
// O(N) search or HashSet<int>.Contains()?
return Enumerable.Contains(collection, needle);
}
Will Enumerable.Contains()
resolve to the optimized implementation on HashSet
or will a simple search be performed regardless of the input?
Yes, it will use HashSet<T>.Contains
. HashSet<T>
implements ICollection<T>
and per the documentation for Enumerable.Contains
:
If the type of source implements
ICollection<T>
, theContains
method in that implementation is invoked to obtain the result. Otherwise, this method determines whether source contains the specified element.
always, Always, ALWAYS check the documentation!
Yes, it does - in some cases. Not always when you think it might be able to though.
As part of writing Edulinq, I wrote two posts (part 40; part 42) on optimization. Basically what counts as a valid optimization isn't always obvious - but there are definitely plenty of cases where LINQ to Object optimizes based on the execution-time type of the collection. This is mostly the case for methods returning a single value rather than a sequence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With