我在我的代码中使用了这些,但我认为它们可能不如手动编码程序快。我搜索并发现一些文章说 System.arraycopy() 实际上比手动复制数组更快。我不太确定这是否正确。
另外,Array.sort() 函数比我们写的代码快吗?
// I am merging the arrays here into a new integer array called newarray3
int[] newarray3= new int[input1.length + input2.length];
System.arraycopy(input1, 0, newarray3, 0, input1.length);
System.arraycopy(input2, 0, newarray3, input1.length, input2.length);
//sorting the array.
Arrays.sort(newarray3);
input1 和input2 是两个数组,先合并再排序。我想知道以这种方式编码是否会使我的程序变慢。或者它可能是别的东西。请帮忙。
最佳答案
System.arraycopy 在几乎所有情况下都比您手动执行的操作更快,因为它可以执行“批量”数据移动,而不是一次一个元素。主要的异常(exception)是相对较小的数组,因为 arraycopy 中的初始处理(选择要使用的算法)非常重要。
重新排序,不存在在所有条件下都是最优的单一排序算法。
关于java - System.arraycopy() 和 Array.sort() 比 Java 中的手动过程快还是慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15863384/