K
Kevin
Hi all,
I have a question of: is there any way we can create a
multi-dimensional arrays data structure, each grid item is a "int",
with fast access for operations like "increase value in one grid", or
get that value?
I did some tests using two methods, both to my mind are not fast
enough. Details as:
Assumptions:
1) the operations we want to optimize are (faster better):
set/increase/get the int values in the multi-dimensional array.
2) the data is dense.
3) the total dimension number is known in advance.
4) the size for each dimension is also known in advance (and not large,
usually 4-10).
Method 1 I tried (using 2 dimension as example):
create the array like:
int[][] data = new int[sizeA][sizeB];
in this way, we can access the values like:
int v = data[1][3];
data[1][3] = data[1][3] + 1;
data[1][3] = 123;
However, I noticed this method is not fast enough.
So I am thinking maybe the data are not in a continuous memory space,
which making access them slow.
Method 2 I tried (using 2 dimensions as example):
int[] data = new int[sizeA * sizeB];
each time I want to access a data with (indexA, indexB), I use:
int pos = indexA*sizeB + indexB.
int v= data[pos];
data[pos] = data[pos]+1;
data[pos] = 123;
However, I observed the second method does NOT improve the speed of the
data access (mainly in my program, this "data[1][3] = data[1][3] +1"
kind of data access).
So I am wondering do we have any other faster method? (or these 2
methods are already the best methods, I am just asking too much?)
Thanks a lot.
I have a question of: is there any way we can create a
multi-dimensional arrays data structure, each grid item is a "int",
with fast access for operations like "increase value in one grid", or
get that value?
I did some tests using two methods, both to my mind are not fast
enough. Details as:
Assumptions:
1) the operations we want to optimize are (faster better):
set/increase/get the int values in the multi-dimensional array.
2) the data is dense.
3) the total dimension number is known in advance.
4) the size for each dimension is also known in advance (and not large,
usually 4-10).
Method 1 I tried (using 2 dimension as example):
create the array like:
int[][] data = new int[sizeA][sizeB];
in this way, we can access the values like:
int v = data[1][3];
data[1][3] = data[1][3] + 1;
data[1][3] = 123;
However, I noticed this method is not fast enough.
So I am thinking maybe the data are not in a continuous memory space,
which making access them slow.
Method 2 I tried (using 2 dimensions as example):
int[] data = new int[sizeA * sizeB];
each time I want to access a data with (indexA, indexB), I use:
int pos = indexA*sizeB + indexB.
int v= data[pos];
data[pos] = data[pos]+1;
data[pos] = 123;
However, I observed the second method does NOT improve the speed of the
data access (mainly in my program, this "data[1][3] = data[1][3] +1"
kind of data access).
So I am wondering do we have any other faster method? (or these 2
methods are already the best methods, I am just asking too much?)
Thanks a lot.