I’m trying to work out the fastest way to iterate a multidimensional C# array. I have stripped out all the domain code to focus on the problem. At the moment, this executes in 1.86 seconds, performing about 25,000,000 iterations in that time, processing 5000 array elements. I’m setting myself the goal of bring 1.86s down as much as I can in 2 days 🙂
In the real world, it’ll be more like 50,000² to process.
I have tried using PLINQ, but it seems the threading overheads actually make it slowers (comes it at 3.48s).
I’m thinking unsafe C# might be the way to go, however, before I go down that road, I’d appreciate any thoughts about how to improve performance? I haven’t done unsafe C# before, so I’m not sure whether it’d be a performance gain?!
Console.WriteLine("started");
var sw = System.Diagnostics.Stopwatch.StartNew();
long iterations = 0;
string[] data = new string[5000];
string[] data2 = new string[5000];
string[] data3 = new string[5000];
int ubound = data.GetUpperBound(0);
for (int i = 0; i <= ubound; i++)
{
string d1 = data[i];
string d2 = data2[i];
string d3 = data3[i];
for (int j = 0; j < ubound; j++)
{
string e1 = data[j];
string e2 = data2[j];
string e3 = data3[j];
Interlocked.Increment(ref iterations);
}
Interlocked.Increment(ref iterations);
}
Console.WriteLine("Finished {0} iterations in {1} seconds", iterations, sw.Elapsed.TotalSeconds);
Instead of a multidimensional array, go for a flat array and use maths to address it: