Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我认为编译器会将2d数组转换为1d数组,为什么即使考虑缓存未命中也会更快呢? 我在这里阅读了上面的文章,性能和优化,有很多关于二维数组性能的文章,它们大多是C和C++,最好的做法是转换X和Y迭代来限制缓存缺失。_C#_Arrays_Performance_Optimization - Fatal编程技术网

C# 我认为编译器会将2d数组转换为1d数组,为什么即使考虑缓存未命中也会更快呢? 我在这里阅读了上面的文章,性能和优化,有很多关于二维数组性能的文章,它们大多是C和C++,最好的做法是转换X和Y迭代来限制缓存缺失。

C# 我认为编译器会将2d数组转换为1d数组,为什么即使考虑缓存未命中也会更快呢? 我在这里阅读了上面的文章,性能和优化,有很多关于二维数组性能的文章,它们大多是C和C++,最好的做法是转换X和Y迭代来限制缓存缺失。,c#,arrays,performance,optimization,C#,Arrays,Performance,Optimization,我在C#中也尝试了同样的方法,甚至尝试了一种2d数组的一维方法,速度要快得多。为什么我的1d版本比(最佳?)2d版本快很多?我是否生成更少的缓存未命中 设置: int[,] testArr = new int[testSize, testSize]; int[] testArr1Dim = new int[testSize * testSize]; Random r = new Random(); for (int x = 0; x < testSize; x++) { for (

我在C#中也尝试了同样的方法,甚至尝试了一种2d数组的一维方法,速度要快得多。为什么我的1d版本比(最佳?)2d版本快很多?我是否生成更少的缓存未命中

设置:

int[,] testArr = new int[testSize, testSize];
int[] testArr1Dim = new int[testSize * testSize];
Random r = new Random();
for (int x = 0; x < testSize; x++)
{
    for (int y = 0; y < testSize; y++)
    {
        int tmp = r.Next();
        testArr[x, y] = tmp;
        testArr1Dim[(y + x * testSize)] = tmp;
    }
 }
int[,]testArr=newint[testSize,testSize];
int[]testArr1Dim=新int[testSize*testSize];
随机r=新随机();
对于(int x=0;x
缓慢的2d阵列迭代:

 for (int y = 0; y < testSize; y++)
 {
                for (int x = 0; x < testSize; x++)
                {
                    counter += testArr[x, y]+2;
                }
  }
for(int y=0;y
快速2d阵列迭代:比慢速2d阵列的速度快一倍多一点

 for (int x = 0; x < testSize; x++)
 {
                for (int y = 0; y < testSize; y++)
                {
                    counter += testArr[x, y]+2;
                }
  }
for(int x=0;x
1d阵列迭代:比快速2d阵列的速度快一倍多一点

for (int x = 0; x < testSize; x++)
{
                for (int y = 0; y < testSize; y++)
                {
                    counter += testArr1Dim[(y + x * testSize)]+2;
                }
}
for(int x=0;x
HansPassant建议使用锯齿阵列:比1d阵列慢约10%

for (int x = 0; x < testSize; x++)
{
                for (int y = 0; y < testSize; y++)
                {
                    counter += jaggedArr[x][y]+2;
                }
}
for(int x=0;x
HansPassant建议提升锯齿阵列:这比1d方法更快

for (int x = 0; x < testSize; x++)
{
                var col = jaggedArr[x];
                for (int y = 0; y < testSize; y++)
                {
                    counter +=col[y]+2;
                }
}
for(int x=0;x
这在web上有很好的记录。JIT并没有对多维数组进行太多优化。谷歌搜索的结果会比这里提供的答案多得多(只会重复其他答案)。第一个版本的速度很慢,因为它不能有效地使用处理器缓存。在第二个版本中修复了这个问题,现在可以按存储顺序寻址内存。第三个版本更快,因为您避免了对为多维数组编制索引的CLR帮助器方法的调用,并且只需支付一个索引越界检查,而不是两个。对于快速的多维数组,总是倾向于使用锯齿状数组,您需要int[][]。也许您没有将外部数组访问从循环中提升出来。这将使交错数组访问完全等同于向量数组访问。但总体而言,<15%的绩效差异在统计上没有意义,一个错误的分支目标已经足以产生可测量的差异。我认为你已经得到了完成你自己开始的Q+a所需要的东西。只需写你自己的帖子,并将其标记为结束你的问题的答案,这对其他程序员应该是有用的。它会稍微大一些,因为外部数组存储对内部数组的引用。您的测试应该证明差异可以忽略不计:)