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.
我有一个类的2d数组。数组的大小非常大(大约3000 * 3000),使用普通的行和列方法访问数组需要花费很多时间。为此,我想使用指针来访问数组。
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.
我想用指针访问boxData。
Thanks
2 个解决方案
#1
2
Try a jagged array instead of a multi dimensional one, they are faster in Microsoft's CLR implementation
尝试锯齿状阵列而不是多维阵列,它们在Microsoft的CLR实现中更快
BoxData[][] boxData=new BoxData[3000][];
for (int i=0; i<3000; i++)
boxData[i] = new BoxData[3000];
#2
2
Maybe you could use a struct instead of a class for BoxData ?
也许你可以使用结构而不是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.
Struct是一种值类型:当您声明数组时,所有内容都将被填充。您将不再使用循环来创建新的BoxData()实例。
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 vs class限制,你必须以这种方式删除初始化程序...
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;
#1
2
Try a jagged array instead of a multi dimensional one, they are faster in Microsoft's CLR implementation
尝试锯齿状阵列而不是多维阵列,它们在Microsoft的CLR实现中更快
BoxData[][] boxData=new BoxData[3000][];
for (int i=0; i<3000; i++)
boxData[i] = new BoxData[3000];
#2
2
Maybe you could use a struct instead of a class for BoxData ?
也许你可以使用结构而不是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.
Struct是一种值类型:当您声明数组时,所有内容都将被填充。您将不再使用循环来创建新的BoxData()实例。
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 vs class限制,你必须以这种方式删除初始化程序...
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;