I was reviewing a piece of LINQ to Object Join query of one of my colleagues and a code that is written like this (example, not the exact code, the actual query was complex then this…)...
(from i in col1 join j in col2 on i equals j select i).ToList();
where i == j select i).ToList();
my Colleague, argued that Microsoft would have implemented the JOIN operator in a better way then a 2 loops running on each other…
I ran a few test on the 2 queries above, and the query that used the JOIN operator was way faster…
The reason is that the JOIN operator (the extension method) is implemented such that the outer collection’s key is enumerated once and put into a hash table, next the inner collection is iterated and it’s key is checked to see if it’s there in the hashtable, this is just O(1) operation, this is way faster then just comparing a variable in an outer loop with something in an inner loop.
Thank you Rajiv…
Comments
Post a Comment