Simple Scenario : Finding Sum
We have a list of ten thousand numbers and finding the sum n times. The loop will be going for n times each time incrementing the list count.
The sum method with Lambda Expression will be:
private decimal GetSum_Lambda()
{
return list1.Sum(); // Using pre-defined method
}
The core sum method without Lambda Expression will be:
private decimal GetSum_WithoutLambda()
{
decimal sum = 0;
foreach (decimal d in list2)
sum += d;
return sum;
}
The output is graphically depicted as following.


The red lines in x axis shows the processing time and y axis shows increasing complexity of the test. Both test complexity are equally distributed.
The above test shows that Lamda expressions for the scenario is almost same. Not too much you might wonder – I agree. But in the following test you will be seeing some surprising outputs.
Complex Scenario : Finding Distinct and then Sum
Shall we move towards a complex scenario?
Here the list has duplicate numbers. We need to remove the duplicate numbers and calculate the sum. This involves the following steps.
1. Remove the duplicates and create the new list
2. Find the sum from new list
The distinct and sum method with lambda expression looks like:
private decimal GetDistinctSum_Lambda()
{
return list1.Distinct().Sum();
}
The distinct and sum method without using lambda expression would be:
private decimal GetDistinctSum_WithoutLambda()
{
// Find Distinct
List<decimal> distinctList = new List<decimal>();
foreach (decimal number in list2)
if (!distinctList.Contains(number))
distinctList.Add(number);
// Find Sum
decimal sum = 0;
foreach (decimal d in distinctList)
sum += d;
return sum;
}
The output is graphically depicted as following.

Total Time for Lambda: 1 second
Total Time without Lambda: 11 seconds
From the above picture shows that Lambda expression is multiple times faster than the usual approach, as well it is memory efficient too.
Test Application
In the test application, both the simple and complex scenario can be tested using the combo box.
Click the Start button to start the computations. First the left panel computations are executed and then the right. Hope you can execute it.
Please let me know your suggestions or comments on the article.
Note
If we try many examples like this we can identify Lambda is having multiple time performance benefits than traditional programming. So I believe it is worth investing in Functional Programming in C# through Lambda Expressions. Happy Coding.

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.