Performance: Memory Locality matters

Programming C#

Let’s assume we have an array of arrays. Effectively it’s a table, let's say 5000×5000 in size. We want to count how many slots have a value greater than zero.

Question – which of these two is faster?

for (int i = 0; i < myArray.Length; i++)
{
for (int n = 0; n < myArray.Length; n++)
{
if (myArray[i][n] > 0)
{
result++;
}
}
}
for (int i = 0; i < myArray.Length; i++)
{
for (int n = 0; n < myArray.Length; n++)
{
if (myArray[n][i] > 0)
{
result++;
}
}
}

Answer? The first one. How much so? Well quite aboute 7x till 8x performance improvement on this loop!

Notice the difference? It’s the order that we’re walking this array of arrays [i][n] vs. [n][i]. Memory locality does indeed matter in .NET even though we’re well abstracted from managing memory ourselves.