C# Array Optimization Tip

Performance optimization

You want to optimize the array element assignments and initializations in your C# program. While typically arrays are initialized in a for-loop, this introduces the loop overhead to the operation, and using constant array element assignment assignment expressions can be faster, depending on the size of the array.

Program

Note

To start, we look at a benchmark harness written in the C# programming language that tests operations on a single array. The first method, Method1, uses a for-loop to loop over the first twelve elements of the parameter array and assign each element to a certain value. The second method, Method2, uses twelve assignment statements, and results in more source code statements and lines but has faster performance.

Program that benchmarks array element assignments [C#]

using System;
using System.Diagnostics;

class Program
{
    const int _max = 100000000;
    static void Main()
    {
	int[] array = new int[12];
	Method1(array);
	Method2(array);

	var s1 = Stopwatch.StartNew();
	for (int i = 0; i < _max; i++)
	{
	    Method1(array);
	}
	s1.Stop();
	var s2 = Stopwatch.StartNew();
	for (int i = 0; i < _max; i++)
	{
	    Method2(array);
	}
	s2.Stop();
	Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) /
	    _max).ToString("0.00 ns"));
	Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) /
	    _max).ToString("0.00 ns"));
	Console.Read();
    }

    static void Method1(int[] array)
    {
	// Initialize each element in for-loop.
	for (int i = 0; i < array.Length; i++)
	{
	    array[i] = 1;
	}
    }

    static void Method2(int[] array)
    {
	// Initialize each element in separate statement with no enclosing loop.
	array[0] = 1;
	array[1] = 1;
	array[2] = 1;
	array[3] = 1;
	array[4] = 1;
	array[5] = 1;
	array[6] = 1;
	array[7] = 1;
	array[8] = 1;
	array[9] = 1;
	array[10] = 1;
	array[11] = 1;
    }
}

Output

11.48 ns
5.43 ns

Method1 versus Method2. In the program source text, the methods Method1 and Method2 both copy a reference (four byte) variable to the formal parameter slot when called (if not inlined). Then, Method1 invokes the for-loop overhead, using an iteration variable (induction variable).

Each store operation into the array uses a affine expression to compute the physical location of the element desired. The difference between Method1 and Method2 is that Method1 uses a explicit loop structure, while Method2 uses an unrolled or unwound loop structure.

Longer code is faster?

Question and answer

Here, you may be wondering why a method with twelve lines of code is more than twice as fast as a method with three lines of code. After all, each method computes the exact same result in the program.

The reason the longer method is faster is that we are actually outsmarting the compiler: the C# compiler is unable to determine that a for-loop over twelve elements is slower than twelve explicit assignments. Please remember that it is more difficult to construct a versatile compiler than write little C# programs.

Summary

The C# programming language

We examined an optimization in the C# language that describes array initialization or assignment procedures. We found that a method that is longer is faster than a method that is shorter, even when both use the same basic assignment instructions. This basic idea is also detailed in the book Code Complete by Steve McConnell; further, this optimization is used in many programs to avoid loop overhead, and is sometimes called loop unrolling.

Code Complete: Book Review Loop Unwinding Array Types
.NET