我有一些奇怪的性能结果,我无法完全解释。这条线好像
d = new double[4, 4]{{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},};
比这个慢 4 倍
d = new double[4, 4];
d[0, 0] = 1; d[0, 1] = 0; d[0, 2] = 0; d[0, 3] = 0;
d[1, 0] = 0; d[1, 1] = 1; d[1, 2] = 0; d[1, 3] = 0;
d[2, 0] = 0; d[2, 1] = 0; d[2, 2] = 1; d[2, 3] = 0;
d[3, 0] = 0; d[3, 1] = 0; d[3, 2] = 0; d[3, 3] = 1;
(这甚至没有考虑到在这个例子中我可以省略所有这些= 0
任务)
我知道由于边界检查,在 c# 中循环多维数组可能会很慢。但是这里没有循环,不需要边界检查,整个数组初始化行可以在编译时解析。
然而,第二个代码块必须首先将数组初始化为零,然后单独覆盖每个值。
那么这里的问题是什么?
如果性能是一个问题,那么初始化这个数组的最佳方法是什么?
我使用以下代码来衡量性能:
using System;
using System.Diagnostics;
class Program
{
public static double[,] d; // global static variable to prevent the JIT optimizing it away
static void Main(string[] args)
{
Stopwatch watch;
int numIter = 10000000; // repeat all tests this often
double[,] d2 = new double[4, 4]{{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},};
// ================================================================
// use arrayInitializer: slowest
watch = Stopwatch.StartNew();
for (int i = 0; i < numIter; i++)
{
d = new double[4, 4]{{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},};
}
Console.WriteLine("ArrayInitializer: \t{0:0.##########}ms", watch.ElapsedMilliseconds * 1.0 / numIter);
// ================================================================
// use Array.Copy: faster
watch = Stopwatch.StartNew();
for (int i = 0; i < numIter; i++)
{
d = new double[4, 4];
Array.Copy(d2, d, d2.Length);
}
Console.WriteLine("new + Array.Copy: \t{0:0.##########}ms", watch.ElapsedMilliseconds * 1.0 / numIter);
// ================================================================
// direct assignment: fastest
watch = Stopwatch.StartNew();
for (int i = 0; i < numIter; i++)
{
d = new double[4, 4];
d[0, 0] = 1; d[0, 1] = 0; d[0, 2] = 0; d[0, 3] = 0;
d[1, 0] = 0; d[1, 1] = 1; d[1, 2] = 0; d[1, 3] = 0;
d[2, 0] = 0; d[2, 1] = 0; d[2, 2] = 1; d[2, 3] = 0;
d[3, 0] = 0; d[3, 1] = 0; d[3, 2] = 0; d[3, 3] = 1;
}
Console.WriteLine("direct assignment: \t{0:0.##########}ms", watch.ElapsedMilliseconds * 1.0 / numIter);
}
}
结果:
ArrayInitializer: 0,0007917ms
new + Array.Copy: 0,0002739ms
direct assignment: 0,0002281ms