SilverShaded
Well-known member
- Joined
- Mar 7, 2020
- Messages
- 110
- Programming Experience
- 10+
Hello, i have a program which is heavy on array access, so i've been tyring to optimise the speed. This is important for the overall program which is highly iterative with lots of matrix manipulation. I was hoping to get any criticism of the code below or any further suggestions to speed it up more.
Current run times (in release mode) are respectively, in ms, the relative time differences are relatively constant, at the moment option 4 is looking like the best possible, which represent a significant increase in speed over option 2 which is the current method in the progam. Before i go changing a whole bunch of arrays are there any obvious improvements possible or better ways to do it or any problems with the code below that may give odd results?
elapsedMs1 128
elapsedMs2 29
elapsedMs3 228
elapsedMs4 7
Current run times (in release mode) are respectively, in ms, the relative time differences are relatively constant, at the moment option 4 is looking like the best possible, which represent a significant increase in speed over option 2 which is the current method in the progam. Before i go changing a whole bunch of arrays are there any obvious improvements possible or better ways to do it or any problems with the code below that may give odd results?
elapsedMs1 128
elapsedMs2 29
elapsedMs3 228
elapsedMs4 7
Array Access Speed Test:
[TestMethod]
[MethodImpl(MethodImplOptions.NoInlining)]
public unsafe void TestDoubleDoubleArrayWithPts()
{
double[,] Array = new double[10000,1000];
var watch = Stopwatch.StartNew();
int UpperBound0 = Array.GetUpperBound(0);
int UpperBound1 = Array.GetUpperBound(1);
watch.Restart();
for (int i = 0; i < Array.GetUpperBound(0); i++)
for (int y = 0; y < Array.GetUpperBound(1); y++)
Array[i,y] = 10D * 10D;
var elapsedMs1 = watch.ElapsedMilliseconds;
watch.Restart();
for (int i = 0; i < UpperBound0; i++)
for (int y = 0; y < UpperBound1; y++)
Array[i, y] = 10D * 10D;
var elapsedMs2 = watch.ElapsedMilliseconds;
watch.Restart();
fixed (double* ptr = Array)
{
for (int i = 0; i < Array.GetUpperBound(0); i++)
for (int y = 0; y < Array.GetUpperBound(1); y++)
*(ptr + i * Array.GetUpperBound(1) + y) = 10D * 10D;
}
var elapsedMs3 = watch.ElapsedMilliseconds;
watch.Restart();
fixed (double* ptr = Array)
{
for (int i = 0; i < UpperBound0; i++)
for (int y = 0; y < UpperBound1; y++)
*(ptr + i*UpperBound1 + y) = 10D * 10D;
}
var elapsedMs4 = watch.ElapsedMilliseconds;
System.Windows.Forms.MessageBox.Show(elapsedMs1.ToString() + " " + elapsedMs2.ToString() + " " + elapsedMs3.ToString() + " " + elapsedMs4.ToString());
}