PERFORMANCE: IEnumerable<>.Sum
Every once in a while you have to dig deep into your application and look for problems! I have a application that wasn't performing! After searching a bit I found my problem:
I do a huge amount of queries and then sum some of the fields in these collections using the Sum extension method (System.Linq.Enumerable). This does make the code easier to read but I never realized the performance hit!!!
To test this, I wrote a very simple application with a list of employees.
class Employee
{
public int Age { get; set; }
}
Each employee is assigned a random age
Random rnd = new Random();
List<Employee> employees = new List<Employee>();
for (int i = 0; i < 1000000; i++)
employees.Add(new Employee() { Age = rnd.Next(18, 80) });
Next, I calculate the sum of each employee's age using 4 different methods.
Method 1: IEnumerable<>.Sum
sum = employees.Sum(e => e.Age);
Method 2: for
for (int i = 0; i < employees.Count; i++)
{
sum += employees[ i ].Age;
}
Method 3: foreach
foreach (var e in employees)
{
sum += e.Age;
}
Method 4: IEnumerable<>.ForEach
employees.ForEach(delegate(Employee e)
{
sum += e.Age;
});
The results
Based on my tests, IEnumerable<>.ForEach, foreach and for performed basically the same (for was the slowest, 17% slower than IEnumerable<>.ForEach), but what was vary scary is that the IEnumerable<>.Sum was on average 92% slower!!!
Conclusion
Although the syntax of the new IEnumerable<>.Sum simplify code, the performance hit MUST not be forgotten!!!