使用System.arraycopy(...)比复制数组的for循环更好吗? [英] Is it better to use System.arraycopy(...) than a for loop for copying arrays?

查看:112
本文介绍了使用System.arraycopy(...)比复制数组的for循环更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个新的对象数组,将两个较小的数组放在一起。

I want to create a new array of objects putting together two smaller arrays.

它们不能为空,但大小可能为0.

They can't be null, but size may be 0.

我不能在这两种方式之间做出选择:它们是等效还是效率更高(例如system.arraycopy()复制整个块)?

I can't chose between these two ways: are they equivalent or is one more efficient (for example system.arraycopy() copies whole chunks)?

MyObject[] things = new MyObject[publicThings.length+privateThings.length];
System.arraycopy(publicThings, 0, things, 0, publicThings.length);
System.arraycopy(privateThings, 0, things,  publicThings.length, privateThings.length);

MyObject[] things = new MyObject[publicThings.length+privateThings.length];
for (int i = 0; i < things.length; i++) {
    if (i<publicThings.length){
        things[i] = publicThings[i]
    } else {
        things[i] = privateThings[i-publicThings.length]        
    }
}

代码外观的唯一区别是什么?

Is the only difference the look of the code?

编辑:感谢链接问题,但是他们似乎有一个未解决的讨论:

thanks for linked question, but they seem to have an unsolved discussion:

如果它不是本机类型:byte [它真的更快吗? ],Object [],char []?在所有其他情况下,执行类型检查,这将是我的情况,因此将是等同的......不?

Is it truly faster if it is not for native types: byte[], Object[], char[]? in all other cases, a type check is executed, which would be my case and so would be equivalent... no?

在另一个相关问题上,他们说大小重要,大小> 24 system.arraycopy()获胜,小于10,手动循环更好...

On another linked question, they say that the size matters a lot, for size >24 system.arraycopy() wins, for smaller than 10, manual for loop is better...

现在我真的很困惑。

推荐答案

public void testHardCopyBytes()
{
    byte[] bytes = new byte[0x5000000]; /*~83mb buffer*/
    byte[] out = new byte[bytes.length];
    for(int i = 0; i < out.length; i++)
    {
        out[i] = bytes[i];
    }
}

public void testArrayCopyBytes()
{
    byte[] bytes = new byte[0x5000000]; /*~83mb buffer*/
    byte[] out = new byte[bytes.length];
    System.arraycopy(bytes, 0, out, 0, out.length);
}

我知道JUnit测试不是最适合基准测试的,但是

testHardCopyBytes需要0.157秒来完成



testArrayCopyBytes需要0.086秒来完成。



我认为它取决于虚拟机,但它看起来好像是复制内存块而不是复制单个数组元素。这绝对会提高性能。


I know JUnit tests aren't really the best for benchmarking, but
testHardCopyBytes took 0.157s to complete
and
testArrayCopyBytes took 0.086s to complete.

I think it depends on the virtual machine, but it looks as if it copies blocks of memory instead of copying single array elements. This would absolutely increase performance.

编辑:

看起来System.arraycopy的性能已经到处都是。
当使用字符串而不是字节,并且数组很小(大小为10)时,
我得到以下结果:


It looks like System.arraycopy 's performance is all over the place. When Strings are used instead of bytes, and arrays are small (size 10), I get these results:

    String HC:  60306 ns
    String AC:  4812 ns
    byte HC:    4490 ns
    byte AC:    9945 ns

这是数组大小为0x1000000时的样子。看起来System.arraycopy肯定会赢得更大的数组。

Here is what it looks like when arrays are at size 0x1000000. It looks like System.arraycopy definitely wins with larger arrays.

    Strs HC:  51730575 ns
    Strs AC:  24033154 ns
    Bytes HC: 28521827 ns
    Bytes AC: 5264961 ns

多么奇特!


感谢Daren,他指出引用的副本不同。这使得这个问题变得更加有趣!

How peculiar!

Thanks, Daren, for pointing out that references copy differently. It made this a much more interesting problem!

这篇关于使用System.arraycopy(...)比复制数组的for循环更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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