创建网格后,大小将保持不变(所有网格的大小相同),并且与每个单元格关联的对象不会改变
那么更有效的是多维数组:
// Creates array
Cell[,] array = new Cell[20, 50];
// Access to items (get item by X/Y indexes)
array[10, 12] = ...;
想知道 Cell[,] 与 Dictionary, Cell> 方法的比较
Cell[,]
HashTable
每次要访问项目时都不需要执行查找,因此效率更高。让我们写一些基准来给出一些想法(注意它可能不是最好的基准,但至少它给出了一些想法):
internal class Program
{
private static void Main(string[] args)
{
Stopwatch sw = Stopwatch.StartNew();
int[,] array = null;
for (int i = 0; i < 1000; i++)
{
array = ArrayInit();
}
sw.Stop();
Console.WriteLine("ArrayInit: {0}", sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
Dictionary<Tuple<sbyte, sbyte>, int> dic = null;
for (int i = 0; i < 1000; i++)
{
dic = DictionaryInit();
}
sw.Stop();
Console.WriteLine("DictionaryInit: {0}", sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
int res;
for (int i = 0; i < 1000000; i++)
{
res = ArrayLookup(array);
}
sw.Stop();
Console.WriteLine("ArrayLookup: {0}", sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
for (int i = 0; i < 1000000; i++)
{
res = DictionaryLookup(dic);
}
sw.Stop();
Console.WriteLine("DictionaryLookup: {0}", sw.ElapsedMilliseconds);
Console.Read();
}
private static int[,] ArrayInit()
{
int[,] array = new int[50, 50];
for (sbyte x = 0; x < 50; x++)
{
for (sbyte y = 0; y < 50; y++)
{
array[x, y] = x * y;
}
}
return array;
}
private static int ArrayLookup(int[,] array)
{
return array[12, 12];
}
private static int DictionaryLookup(Dictionary<Tuple<sbyte, sbyte>, int> dic)
{
return dic[new Tuple<sbyte, sbyte>(12, 12)];
}
private static Dictionary<Tuple<sbyte, sbyte>, int> DictionaryInit()
{
Dictionary<Tuple<sbyte, sbyte>, int> dic = new Dictionary<Tuple<sbyte, sbyte>, int>();
for (sbyte x = 0; x < 50; x++)
{
for (sbyte y = 0; y < 50; y++)
{
Tuple<sbyte, sbyte> t = new Tuple<sbyte, sbyte>(x, y);
dic[t] = x * y;
}
}
return dic;
}
结果:
数组初始化:25
字典初始化:528
数组查找:7
字典查找:326