J
JP
Hi Guys,
I am venturing into the realm of scientific computing using C#. And on
that subject I have a question for you!
I have a two dimensional matrix that I need to address very
frequently. The size of the matrix the smallest being 2,000 * 2,000
and the largest being 5,000 * 500,000. Thus the number of cells range
from 4,000,000 to 1,000,000,000 cells.
The content of the cells is limited to the values 0, 1 and 2.
I know that using a rectangular array is pretty fast, but using a
single-dimensional array is still faster. So I want to use this
approach to describe the matrix:
byte[] myArray = new byte[ROW_DIM * COLUMN_DIM];
and reading the value like this:
byte val = myArray[row * COLUMN_DIM + column];
I am not sure about what type of collection I should use though.
Suggestions and explanations are very welcome!
I am also a little in doubt as to how to conserve memory. What
datatype should I use to represent the values in the cell. They can
only ever be 0, 1 or 2. So should I represent them using a byte or
perhaps a nullable boolean?
The last thing I am considering is to reverse the problem somehow.
Could I represent the matrix from the view of the values instead of
the cells. By that I mean maybe three arrays or collections (one for
each possible value) that somehow hold an index of the cells
containing this value. Would this conserve mamory or make it faster to
address the matrix? I can't really see a smart way of doing this, but
if anyone knows of a smart way, then I am all ears!
Thanks,
JP
I am venturing into the realm of scientific computing using C#. And on
that subject I have a question for you!
I have a two dimensional matrix that I need to address very
frequently. The size of the matrix the smallest being 2,000 * 2,000
and the largest being 5,000 * 500,000. Thus the number of cells range
from 4,000,000 to 1,000,000,000 cells.
The content of the cells is limited to the values 0, 1 and 2.
I know that using a rectangular array is pretty fast, but using a
single-dimensional array is still faster. So I want to use this
approach to describe the matrix:
byte[] myArray = new byte[ROW_DIM * COLUMN_DIM];
and reading the value like this:
byte val = myArray[row * COLUMN_DIM + column];
I am not sure about what type of collection I should use though.
Suggestions and explanations are very welcome!
I am also a little in doubt as to how to conserve memory. What
datatype should I use to represent the values in the cell. They can
only ever be 0, 1 or 2. So should I represent them using a byte or
perhaps a nullable boolean?
The last thing I am considering is to reverse the problem somehow.
Could I represent the matrix from the view of the values instead of
the cells. By that I mean maybe three arrays or collections (one for
each possible value) that somehow hold an index of the cells
containing this value. Would this conserve mamory or make it faster to
address the matrix? I can't really see a smart way of doing this, but
if anyone knows of a smart way, then I am all ears!
Thanks,
JP