1

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

7
  • 1
    no you don't!...:) Suggest you post the (timed) code that is causing the problem. Commented Oct 26, 2010 at 9:12
  • Have you tried to use a Key Value Dictionary, using a key that is calculated using XY coordinates ? Commented Oct 26, 2010 at 9:19
  • @Mitch: I am just getting the bounds and colors from the array using loop. Commented Oct 26, 2010 at 9:31
  • @controlbreak: No, I didn't tried Key Value. How can I use it as arrays??? Commented Oct 26, 2010 at 9:33
  • I am surprised, accessing an array is very fast. Do you mean it takes time to populate it the first time ? (3000x3000 iterations ?) Commented Oct 26, 2010 at 9:40

2 Answers 2

2

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];     
Sign up to request clarification or add additional context in comments.

1 Comment

Good advice, but you should say why.
2

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.