Possible Duplicate:
Which of these two for loops is more efficient in terms of time and cache performance
Why does the order of the loops affect performance when iterating over a 2D array?
I want to know the fastest method of going through a array, in big array I’m using its important. for example i have:
ushort[, , ,] map = new ushort[3000, 3000, 3, 3]; // [pointX, pointY, stack, lvl]
for (int i = 0; i < 3000; i++)
for (int j = 0; j < 3000; j++)
for (int k = 0; k < 3; k++)
for (int l = 0; l < 3; l++)
map[i, j, k, l] = 45001;
It’s probably relevant as it depends on how those values are stored in memory. So in that case is it better to do this in that order – i->j->k->l or l->k->j->i?
Also, is it different for other languages?
It makes big difference on the way how to initialize the array in java, the following is pure java code to show the difference , you can refer to the code below to improve the efficiency:
result:
As a result, I would like to suggest you to use int [][][][] map = new int[3][3][3000][3000] instead in java.