Clone: , . , , , Size . , , :
- , .
Dictionary<Size, int[,]> ( , Size - ), , Size. .Clone.
, 3, :
1. Width Height , Size .
static int[,] CreateArray(Size size) {
int w = size.Width;
int h = size.Height;
int[,] array = new int[w, h];
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
array[x, y] = int.MaxValue;
}
}
return array;
}
1000x1000, 120000 140000 .
2. , , .
static int[,] CreateArray(Size size) {
int w = size.Width;
int h = size.Height;
int[,] array = new int[w, h];
Action<int[,], int, int> fillFirstHalf = FillArray;
Action<int[,], int, int> fillSecondHalf = FillArray;
var firstResult = fillFirstHalf.BeginInvoke(array, 0, h / 2, null, null);
var secondResult = fillSecondHalf.BeginInvoke(array, h / 2, h, null, null);
fillFirstHalf.EndInvoke(firstResult);
fillSecondHalf.EndInvoke(secondResult);
return array;
}
static void FillArray(int[,] array, int ystart, int yend) {
int w = array.GetLength(0);
for (int x = 0; x < w; ++x) {
for (int y = ystart; y < yend; ++y) {
array[x, y] = int.MaxValue;
}
}
}
, , , , 100x100, . 1000x1000 , 70 . ( 120- , , ).
, , (.. , 500 ), , . ; .
3. unsafe.
: , .NET *: , "" , . , 10x2 , 20x1; 10x10 , , 100x1 ..
, . , . , .
* , .NET , , "".
fixed unsafe :
static int[,] CreateArray(Size size) {
int w = size.Width;
int h = size.Height;
int[,] array = new int[w, h];
unsafe {
fixed (int* ptr = array) {
for (int i = 0; i < w * h; ++i)
ptr[i] = int.MaxValue;
}
}
return array;
}
() - CreateArray №2, FillArray :
static void FillArray(int[,] array, int ystart, int yend) {
int w = array.GetLength(0);
unsafe {
fixed (int* p = array) {
for (int i = w * ystart; i < w * yend; ++i)
p[i] = int.MaxValue;
}
}
}
, , , , , unsafe .
stackalloc: , . stackalloc:
expr type , ; ptr. ( fixed). , ( )
, , , stackalloc , .