Comparing different ways to loop through a collection in C#

I think it is very convenient the use of the ForEach statement when we want to loop through a list of items in C#. Simple and concise, easy to read but what is going on behind the scenes? Is the best option? This article will explore each option in detail.

Let’s start:

The old fashioned FOR loop

The for statement executes a statement or a block of statements while a specified Boolean expression evaluates to true. We just need to define a starting point and an increment pattern of the variable used as index.  

        public void ForLoop()
        {
            sum = 0;
            for (int i = 0; i < elements.Count; i++)
            {
                sum += elements[i];
            }
        }

The compiler translates/refactors high-level language constructs to lower-level language constructs. This is important to understand as it could compromise the performance of your code. All these transformations happen before your code is compiled into IL, this step is called Lowering.

After lowering, the code would look like this:

    public void ForLoop()
    {
        sum = 0;
        int num = 0;
        while (num < elements.Count)
        {
            sum += elements[num];
            num++;
        }
    }

Ok, that’s interesting! The for statement has been replaced by while.


The cool kid, ForEach.

The foreach statement executes a statement or a block of statements for each element in an instance of the type that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface.

        public void ForEachLoop()
        {
            sum = 0;
            foreach (var element in elements)
            {
                sum += element;
            }
        }

Without a doubt, this option is easier to read and understand as there is no need of indexes and increment patterns. After lowering, the code looks as follows:

    public void ForEachLoop()
    {
        sum = 0;
        List<int>.Enumerator enumerator = elements.GetEnumerator();
        try
        {
            while (enumerator.MoveNext())
            {
                int current = enumerator.Current;
                sum += current;
            }
        }
        finally
        {
            ((IDisposable)enumerator).Dispose();
        }
    }

OK.. there’s some magic here, the code now includes try/finally in order to dispose the enumerator. However, the while statement remains the same, compared with the previous example.


Even fancier, List.ForEach

This option only applies to collections of type List allowing to execute an action for each element of the list, see more.

        public void ForEachLambdaLoop()
        {
            sum = 0;
            elements.ForEach(
                x => sum += x
            );
        }

Looks nice to be honest, the method is expecting an Action that will be executed for each element in the collection. After lowering this code, this is how it looks like:

    public void ForEachLambdaLoop()
    {
        sum = 0;
        elements.ForEach(new Action<int>(<ForEachLambdaLoop>b__5_0));
    }

    [CompilerGenerated]
    private void <ForEachLambdaLoop>b__5_0(int x)
    {
        sum += x;
    }

Note that a new method to compute the arithmetic operation has been added and also decorated with CompilerGenerated.


How to compare all three options?

To get some metrics, I have created a sample project that you can download and use for your reference. In this case, I am using the BenchmarkDotNet package to analyze the code during its execution.

C:\temp\sharplab>dotnet add package BenchmarkDotNet

I have defined a basic class including three methods for each option discussed above. It is important to execute the code in Release mode for Benchmark to work.

C:\temp\sharplab>dotnet run -c release

The results

It is kind of shocking that a for statement executes in less than half of the time of the other two options. By looking at the code, I already knew that it would be the fastest option, but I had no idea how fast!

Summary

Two things to take away from this article:

By the way, if someone says that your code is not efficient because you are initializing a List and later, adding each element by calling the .Add() function. They are wrong, it is the same thing after lowering.

Visit: https://sharplab.io/

private readonly List<int> elements;
private int sum = 0;

public Loop()
{
    elements = new List<int>() {1,2,3,4,5,6,7,8,9,10};
}
private readonly List<int> elements;

private int sum = 0;

public Loop()
{
  List<int> list = new List<int>();
  list.Add(1);
  list.Add(2);
  list.Add(3);
  list.Add(4);
  list.Add(5);
  list.Add(6);
  list.Add(7);
  list.Add(8);
  list.Add(9);
  list.Add(10);
  elements = list;
}

Cheers!

How to create a resume that make sense?

This articule will cover the scenario of an Software Engineer that has been performing multiple roles for a company throughout the years. How to represent all the activities in a way that actually make sense to the person who is reading the resume or cover letter.

Based on my experiencie working for a large IT Services Company, over the years I was acumulating experiencie working with different customers and their development teams in different roles: developer, technical lead, software architect, scrum master and even as a devops enabler. All this experience is great and I think we can represent it with any hussle on the paper. But, what about the activities that a senior member needs to perform for the actually employer company? For instance, Team Leader, Project Leader, Portfolio Manager, Engagement Manager, Agile Coach, Coach, Trainner, etc.

Inmediately, some questions come in mind:

  • If I am working for my customer as a contractor 8 hours a day, 5 days a weeks and sometimes more. Can I mention that I work for them or should I mention the employer only?
  • In parallel, I have been performing additional task for the actual employer company, how can I include this?
  • How can I represent both things in a document that actually make sense to the reader?

Well, thinking on a solution to represent my working experience, the environment as well as the activities performed by me I started looking a sample CV. As usual, that first time I did it was to open the browser and search in google images. Lot of really nice templates I found but still nothing that I can actually use.

Another thing was bothering me, I wanted to keep the document short in just a single page. Even though I have 15 years of total experiencie in Software Development, I don’t think the first job I did 15 years ago is relevant now because even the technologies that I was using at that time are no longer available or changed completely in today’s world.

Nowadays, having a profile picture on the resume is expected, not mandatory though. Also, including a short description of the thing you are good at it, your interests, how your skills can contribute in the organization you are trying to get in, etc. Having all these in consideration I came up with a Microsoft Word Template, that I think cover most of the points. Please do not hesitate to comment on things that I might be mising.

The idea behind this design is to represent both worlds, “the employee” on the right as well as “the contractor” on the left sections. The timeline is just a way to lead the eye over the activities, the role, the company as well as a description of the environment and the tools that were used.

I already shared this version of my resume for a job opportunity and the feedback I got was: “It was really clear”. So, if you think this design actually make sense to you, feel free to download a copy. It’s fully customizable. By the way, if you are looking to create your first resume with a really good template, you do it online from resume.io.

Thanks for stopping by.

Cheers!