Reputation: 5994
I have multiple float arrays. Their length has not to match with each other. Now I have to merch all them to one single float array. To merch them, I would like to calculate the average value for each index. That means:
output[n] = (input0[n] + input1[n] + input2[n] ... + inputx[n]) / x
All that has to be calculated extremely fast. I don't care whether the code is nice to read or extensible. It just has to be as fast as possible :
I've created the following code:
private void Mix()
{
List<float[]> input = new List<float[]>();
//...
//input has [n] float arrays.
//eg:
//0: [0.5, 0.3, 0.6, 0.1, -0.3, 0.1, -0.9]
//1: [0.1, 0.7, -0.2, 0.8, -0.2]
//2: [-0.3, 0.9, 0.5, 0.4, 0.8, -0.6]
//3: [0.5, -0.2, 0.4]
//-----------------------------------------------
//the average value for the first value would have to be this:
//(0.5 + 0.1 - 0.3 + 0.5) / 4
//mix all together:
//===================
//create an output buffer
int length = input.Max(x => x.Length);
float[] output = new float[length];
for (int n = 0; n < length; n++)
{
float value = 0;
int count = 0;
for (int x = 0; x < input.Count; x++)
{
float[] inputBuffer = input[x]; //select the input array
if (inputBuffer.Length >= n) //check whether there is a value to get
{
value += inputBuffer[n]; //get the value of the input array
count++;
}
}
output[n] = value / count; //calculate the average
}
}
As you can see, it contains nested for loops. I would guess, that this code is by far not fast enough. So is there anything how to make it faster?
Upvotes: 0
Views: 258
Reputation: 5424
Two things come to mind to make it faster:
Rearrange the code so that the if
statement is in the outer loop rather than the inner loop. Think "exit early" rather than "verify that each index is in range".
Leverage the SIMD library here: http://blogs.msdn.com/b/dotnet/archive/2014/04/07/the-jit-finally-proposed-jit-and-simd-are-getting-married.aspx
Upvotes: 1
Reputation: 5144
You might try adding over vectors using a specialized library, see e.g. Recommendation for C# Matrix Library for a list of candidates. As Scott has already argued, that's not going to change the fundamental runtime complexity of your problem, but it might run slightly faster, being optimized for such operations.
Upvotes: 0