I am working on application that requires so many loops and array access to compare arrays. I implemented the same application in c++ and I noticed very big difference in performance please notice I used pointers instead of array index-based access in the c++ version. Then I tried to implement just a small part of my code in both c++ and C#. The C# code is listed below it takes av of 400 ms to execute when measured using stopwatch. Moreover when I increase the limit of the first for to say 10000 the program will never execute just like a deadlock.
int[] A = new int[10000];
int[] B = new int[10000];
Stopwatch sw = new Stopwatch();
sw.Start();
for(int i=0;i<100;i++)
for(int j=0;j<i;j++)
for (int k = 0; k < 10000; k++)
{
A[k] = k;
B[k] = A[k] + j;
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
Console.ReadLine();
my question is how to improve this killing performance of arrays in c# ?
myPC is 4GB RAM core i5 2.2GHz
10,000on the outer loop means you're doing1,000,100,000,000array writes, and500,050,000,000array reads, right? That's not taking into account all the operations on your index variables, either. The problem is your benchmark, not array performance in C#. If the equivalent program you've written in C++ actually completes this, it means the compiler essentially removed half your code which it deemed redundant (the first two loops, for example).