Java的System.arraycopy()对小型数组有效吗? [英] Is Java's System.arraycopy() efficient for small arrays?
问题描述
Java的 System.arraycopy()
对于小型数组是否有效,或者它是一种本机方法的事实使它可能比简单的循环效率低得多一个函数调用?
Is Java's System.arraycopy()
efficient for small arrays, or does the fact that it's a native method make it likely to be substantially less efficient than a simple loop and a function call?
本机方法是否会因跨越某种Java系统桥而产生额外的性能开销?
Do native methods incur additional performance overhead for crossing some kind of Java-system bridge?
推荐答案
稍微扩展一下Sid所写的内容, System.arraycopy
很可能只是一个JIT内在函数;这意味着当代码调用 System.arraycopy
时,它很可能会调用JIT特定的实现(一旦JIT标记 System.arraycopy
因为热而不是通过JNI接口执行,所以它不会产生本机方法的正常开销。
Expanding a little on what Sid has written, it's very likely that System.arraycopy
is just a JIT intrinsic; meaning that when code calls System.arraycopy
, it will most probably be calling a JIT-specific implementation (once the JIT tags System.arraycopy
as being "hot") that is not executed through the JNI interface, so it doesn't incur the normal overhead of native methods.
一般情况下,执行本机方法确实有一些开销(通过JNI接口,当执行本机方法时也不会发生一些内部JVM操作)。但这不是因为一个方法被标记为本机,你实际上是在使用JNI执行它。 JIT可以做一些疯狂的事情。
In general, executing native methods does have some overhead (going through the JNI interface, also some internal JVM operations cannot happen when native methods are being executed). But it's not because a method is marked as "native" that you're actually executing it using JNI. The JIT can do some crazy things.
最简单的检查方法是,正如已经建议的那样,编写一个小基准测试,小心Java微基准测试的常规警告(温暖首先是代码,避免没有副作用的代码,因为JIT只是将它优化为无操作,等等。)
Easiest way to check is, as has been suggested, writing a small benchmark, being careful with the normal caveats of Java microbenchmarks (warm up the code first, avoid code with no side-effects since the JIT just optimizes it as a no-op, etc).
这篇关于Java的System.arraycopy()对小型数组有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!