I need to process about 500,000 data points each consisting of 4 decimals. I'd like to use and array of structs to do this. Would this be much slower than using an array of arrays? It seems that memory won't be an issue, but speed will - it needs to be fast.
Quick code sample of two options:
Option 1:
public struct Struct
{
public decimal A { get; set; }
public decimal B { get; set; }
public decimal C { get; set; }
public decimal D { get; set; }
}
Usage:
private Struct[] data;
Option 2:
private decimal [][] data;
Also, is decimal
the right data type to use? The data points are money...
Thanks! Brian
If you are processing A,B,C,D at the exact same time, the array of structs method should have better spatial locality - since the data is clumped together it will be paged into memory and the same time (fewer page faults) and fetched into the CPU cache at the same time. If you process all of A, then all of B, etc., then the opposite will be true and you should use array of arrays.
If not terribly difficult, I suggest you try both options and measure and see what one is better. If this is too difficult, use whichever approach is simpler and easy to understand and then measure to see if it meets your performance goals.