views:

42

answers:

2

Hi,

I have a 2d array of a class. The size of array is very large (around 3000*3000) and accessing the array with ordinary row and column method is taking very much time. For this purpose, I want to use pointers to access the array.

Following is my array code:

Class definition:

Class BoxData     
{
  Size _bound;
  bool _isFilled=false;
  Color _color=Colors.White;

  public Size Bounds
  {
    get
    {
      return _bound;
    }
    set
    {
      _bound=value;
    }
  }

  public bool IsFilled
  {
    get
    {
      return _isFilled;
    }
    set
    {
      _isFilled=value;
    }
  }

  public Color FillColor
  {
    get
    {
      return _color;
    }
    set
    {
      _color=value;
    }
  }
}

Class used as array in application:

BoxData[,] boxData=new BoxData[3000,3000];

I want to access boxData with pointers.

Thanks

+2  A: 

Try a jagged array instead of a multi dimensional one, they are faster in Microsoft's CLR implementation

BoxData[][] boxData=new BoxData[3000][];
for (int i=0; i<3000; i++)
    boxData[i] = new BoxData[3000];     
ohadsc
Good advice, but you should say why.
Hans Passant
Thanks, I've made it more clear
ohadsc
+2  A: 

Maybe you could use a struct instead of a class for BoxData ?

Struct is a value type: as you declare your array, everything will be populated already. You will not longer use a loop to create new BoxData() instances.

var x = new BoxData[3000,3000]; // Populated array of BoxData

Because of struct vs class restrictions, you will have to remove initializers this way...

struct BoxData
{
    Size _bound;
    bool _isFilled; // = false;
    Color _color; // = Color.White;

    public Size Bounds
    {
        get
        {
            return _bound;
        }
        set
        {
            _bound = value;
        }
    }

    public bool IsFilled
    {
        get
        {
            return _isFilled;
        }
        set
        {
            _isFilled = value;
        }
    }

    public Color FillColor
    {
        get
        {
            return _color;
        }
        set
        {
            _color = value;
        }
    }
}

...and initialize your default values using a loop will be much more faster.

for (int j = 0; j < 3000; j++)
    for (int i = 0; i < 3000; i++)
        x[i, j].FillColor = Color.White;
controlbreak