I am trying to optimize generic lists arithmetic operation. I have 3 lists of nullable double as defined below.
List<double?> list1 = new List<double?>();
List<double?> list2 = new List<double?>();
List<double?> listResult = new List<double?>();
int recordCount = list1.Count > list2.Count ? list2.Count : list1.Count;
for (int index = 0; index < recordCount; index++)
{
double? result = list1[index] + list2[index];
listResult.Add(result);
}
Is there any way to make this operation to run faster if I have huge lists?
Thanks for your input.
You could move your list creation for the results until after your count:
This would let you specify the exact capacity necessary for the results, and avoid reallocations within the list itself. For “huge lists” this is likely one of the slowest portions, as the memory allocations and copies as the list gets large will be the slowest operation here.
Also, if the calculation is simple, you could potentially use multiple cores:
Given that the “work” is so simple here, you probably actually would need a custom partitioner to get the most out of parallelism (see How to: Speed Up Small Loop Bodies for details):
If this isn’t a bottleneck, however, you could use LINQ to do this as a one-liner:
However, this will be (very slightly) less efficient than handling the looping yourself, if performance is really a bottleneck.