C# Loop Over Two Variables

Performance optimization

Loops cannot iterate over individual variables. But with a conditional expression, each iteration can use a different variable. The first iteration of the loop must use variable one. The second iteration must use variable two.

Looping over two variables

Method A - int[]:    1064 ms
Method B - for:       480 ms
Method C - do while:  254 ms [fastest]

Examples

Note

First, you may have encountered this problem often. Your method may receive two separate parameters, but you want only one loop over them. It is often best to refactor the logic into separate methods, but not always. Here we see three code samples that accomplish this task. Each method receives two parameter values, and it must loop over them in one pass.

Loop example 1 [C#]

static int A(int a, int b)
{
    int r = 0;

    int[] arr = new int[2];
    arr[0] = a;
    arr[1] = b;
    for (int i = 0; i <= 1; i++)
    {
	int m = arr[i];
	r += m;
    }
    return r;
}

Loop example 2 [C#]

static int B(int a, int b)
{
    int r = 0;
    for (int i = 0; i <= 1; i++)
    {
	int m = (i == 0) ? a : b;
	r += m;
    }
    return r;
}

Loop example 3 [C#]

static int C(int a, int b)
{
    int r = 0;
    int i = 0;
    int m = a;

    do
    {
	if (i == 1)
	{
	    m = b;
	}
	r += m;
    } while (i++ < 1);

    return r;
}
Letters of the alphabet: ABC

Notes on method A. It creates a temporary array. It loops over each element in that array with for. It sums the total. This is just for the example and isn't the focus. The program uses the for-loop construct in the C# programming language.

For LoopsQuestion and answer

Notes on method B. It receives two ints. The data type isn't the focus here. It loops over 0 and 1. There are two iterations in this loop always. It uses the ternary operator. The question mark acts like a simple if statement. It is equivalent functionally to method A. You can find more information on the ternary operator on this web site.

Ternary Operator Use

Description of method C. It is similar to method B. The difference here is that I refactored the loop to avoid checking the range on the first iteration. It is overly complex. The do/while syntax, and the post-increment syntax is not easy to read.

Compare loops

This section provides information

After showing that the loops work correctly, I benchmarked them for 100 million iterations, and took the average of ten trials. What I found is that creating a temporary array was more than 2x slower than using the ternary operator or the if statement. Method C, which uses the do/while loop, was the fastest. Unfortunately, it also has the hardest to read code.

Refactoring

Often you can refactor your method to call two internal methods and avoid the loop entirely. However, sometimes that makes your code harder to understand due to return values.

Summary

The C# programming language

We looked at ways you can rewrite loops using the C# programming language. These are three ways you can loop over two values. The second and third methods were the fastest. Sometimes it is easier to avoid refactoring code into sub-methods.

Loop Constructs
.NET