是Array.Copy()比循环更快,对于二维数组? [英] Is Array.Copy() faster than for loop, for 2D arrays?

查看:135
本文介绍了是Array.Copy()比循环更快,对于二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更改

        this.FieldValues = new object[2, fieldValues.GetUpperBound(1) + 1];
        for (int i = 0; i < FieldCount; i++)            
        {
            this.FieldValues[Current, i] = fieldValues[Current, i];
            this.FieldValues[Original, i] = fieldValues[Original, i];
        }



to

        FieldValues = new object[2, fieldValues.GetLength(1)];
        Array.Copy(fieldValues, FieldValues, FieldValues.Length); 



WHERE CURRENT和原始的值分别为常数0和1。 FieldValues​​是一个领域,fieldValues​​是一个参数。

Where the values of Current and Original are constants 0 and 1 respectively. FieldValues is a field and fieldValues is a parameter.

在我使用它的地方,我发现Array.Copy()版本要快。但是另一家开发商说,他在一个独立的程序定时对Array.Copy()for循环,发现for循环快。

In the place I was using it, I found the Array.Copy() version to be faster. But another developer says he timed the for-loop against Array.Copy() in a standalone program and found the for-loop faster.

难道Array.Copy ()是不是真的更快?我认为这应该是超优化!

Is it possible that Array.Copy() is not really faster? I thought it was supposed to be super-optimised!

推荐答案

在我自己的经验,我发现,我不能相信我的直觉任何事情,当涉及到性能。所以,我保持一个快速和肮脏的应用标杆周围(即我称之为StupidPerformanceTricks),我用它来测试这些方案。这是非常宝贵的,因为我做了各种关于性能的技巧令人惊讶和反直觉的发现。同样重要的是要记住在发布模式下运行基准测试应用程序,不附加,因为你,否则没有得到JIT优化调试器,以及优化可以使显著差异:技术的一个可能比在调试模式下B技术慢, 。但显著在发布模式,具有优化的代码快

In my own experience, I've found that I can't trust my intuition about anything when it comes to performance. Consequently, I keep a quick-and-dirty benchmarking app around (that I call "StupidPerformanceTricks"), which I use to test these scenarios. This is invaluable, as I've made all sorts of surprising and counter-intuitive discoveries about performance tricks. It's also important to remember to run your benchmark app in release mode, without a debugger attached, as you otherwise don't get JIT optimizations, and those optimizations can make a significant difference: technique A might be slower than technique B in debug mode, but significantly faster in release mode, with optimized code.

这表示,一般来说,我自己的测试经验表明,如果你的阵列为< 〜32元,你会通过滚动自己的副本循环得到更好的性能 - 大概是因为你没有方法调用的开销,这可能是显著做。但是,如果环比32〜较大的元素,你会使用Array.Copy获得更好的性能()。 (如果你复制或整数或浮点数类似的各种各样的事情,你可能还需要调查Buffer.BlockCopy(),这是比〜Array.Copy()为小数组快10%。)

That said, in general, my own testing experience indicates that if your array is < ~32 elements, you'll get better performance by rolling your own copy loop - presumably because you don't have the method call overhead, which can be significant. However, if the loop is larger than ~32 elements, you'll get better performance by using Array.Copy(). (If you're copying ints or floats or similar sorts of things, you might also want to investigate Buffer.BlockCopy(), which is ~10% faster than Array.Copy() for small arrays.)

不过,所有的说,真正的答案是,写尽可能匹配这些精确的替代品,你自己的测试,用循环包装他们每人,给循环足够的迭代它咀嚼向上至少2-3秒值得的CPU,然后比较自己的替代品。

But all that said, the real answer is, "Write your own tests that match these precise alternatives as closely as possible, wrap them each with a loop, give the loop enough iterations for it to chew up at least 2-3 seconds worth of CPU, and then compare the alternatives yourself."

这篇关于是Array.Copy()比循环更快,对于二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆