The text of this article is not in this database — only its details are. The old site it was published on is gone, but the Internet Archive kept a copy: Speed of Lambda
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Tahir AkramPosted Mar 6, 2019, 8:19 PM
Where is the source of test application?
Jean PaulPosted Sep 13, 2012, 9:17 AM
Thank You Lajapathy!
Lajapathy ArunPosted Sep 13, 2012, 8:20 AM
Thanks friend :)
Jean PaulPosted May 11, 2012, 3:22 AM
Thank You Tim!
Tim YangPosted May 11, 2012, 3:21 AM
Study.Very useful.
Stefan NoackeditedPosted Nov 4, 2010, 8:24 AMEdited Nov 4, 2010, 8:26 AM
At least for the first case. Reflector reveals the following implementation of Enumerable.Sum in .NET Framework: public static decimal Sum(this IEnumerable<decimal> source) { if (source == null) { throw Error.ArgumentNull("source"); } decimal num = 0M; foreach (decimal num2 in source) { num += num2; } return num; } I suspect that you ran debug code where nop instructions are inserted between your code lines for the debugger to create breakpoints. I guess there would be no differences if you'd make a realease build with optimizations enabled. The second example is, however, correct. But the used "custom" implementation is of course the least efficient way to solve the problem. Implementing something as fast as the linq expression would take some more code, though. So linq here is really superior. also in the first case i'd prefer linq even if it was slower.
Mahesh ChandPosted Nov 1, 2010, 7:48 PM
I am impressed with the results. I did not think the difference will be this big. Thank you for sharing this. Great job! Best, Mahesh
james.curranPosted Nov 1, 2010, 12:50 PM
I apologize if this is a duplicate (the website seems to drop pending comments when you log in) You should try using a HashSet in GetDistinctSum_WithoutLambda. It really designed for doing that. In fact, you could get much better time, by combining the two loops: private decimal GetDistinctSum_WithoutLambda() { var distinctList = new HashSet<decimal>(); decimal sum = 0; foreach (decimal number in list2) if (!distinctList.Contains(number)) { distinctList.Add(number); sum += number; } return sum; } That shoudl bring you closer to the LINQ functions, since that's largely the way they do it.