I am writing some c# code that is currently supposed to run as fast as possible, usually taking up a single core at 100% for about 25min. I need the code to remain single core as the benefit of running this code across multiple cores will not be as great as running this project multiple times concurrently
The code in question is as follows:
public Double UpdateStuff(){
ClassA[] CAArray = ClassA[*a very large number indeed*];
Double Value = 0;
int length = CAArray.Length;
for (int i= 0; i< length ; i++)
{
Value += CAArray[i].ClassB.Value * CAArray[i].Multiplier;
}
return Value;
}
This area of the code is responsible for 78% of the load of the application according to profilers and thus seems a good candidate for optimisation.
Note, the function has been changed from return type void to return type Double, this is pseudocode and not actual code to allow easier reading.
To Clarify: .net, c#4.0, visual studio 2010, target machine : windows server 2008 x64.
Edit: Further clarification: all variables in this context are public and not properties. The values in CAArray[i].ClassB.Value will be forever changing doubles that cannot be pair matched.
You should remove this:
and replace the loop with this:
Storing the length like your original code does actually slows down C# code (counter-intuitive, I know). This is because if you have Array.Length directly in the for loop, the jitter will skip doing an array bounds-check on each iteration of the loop.
Also, I strongly suggest parallelizing this process. The simplest way to do this is
although you could potentially get even more speed without LINQ (though you then need to worry about the low level details of managing multiple threads).