为什么 System.arraycopy 在 Java 中是原生的? [英] Why is System.arraycopy native in Java?

查看:30
本文介绍了为什么 System.arraycopy 在 Java 中是原生的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶地在 Java 源代码中看到 System.arraycopy 是一个本地方法.

I was surprised to see in the Java source that System.arraycopy is a native method.

当然是因为它更快.但是代码能够采用哪些原生技巧来使其更快?

Of course the reason is because it's faster. But what native tricks is the code able to employ that make it faster?

为什么不循环遍历原始数组并将每个指针复制到新数组——这肯定不是那么慢和麻烦吗?

Why not just loop over the original array and copy each pointer to the new array - surely this isn't that slow and cumbersome?

推荐答案

在原生代码中,可以使用单个 memcpy/memmove,而不是 n 不同的复制操作.性能差异很大.

In native code, it can be done with a single memcpy / memmove, as opposed to n distinct copy operations. The difference in performance is substantial.

这篇关于为什么 System.arraycopy 在 Java 中是原生的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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