.toArray(new MyClass[0]) 还是 .toArray(new MyClass[myList.size()])? [英] .toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?
问题描述
假设我有一个 ArrayList
Assuming I have an ArrayList
ArrayList<MyClass> myList;
而且我想调用toArray,是否有使用的性能原因
And I want to call toArray, is there a performance reason to use
MyClass[] arr = myList.toArray(new MyClass[myList.size()]);
结束
MyClass[] arr = myList.toArray(new MyClass[0]);
?
我更喜欢第二种风格,因为它不那么冗长,而且我认为编译器会确保不会真正创建空数组,但我一直想知道这是否属实.
I prefer the second style, since it's less verbose, and I assumed that the compiler will make sure the empty array doesn't really get created, but I've been wondering if that's true.
当然,在 99% 的情况下,这不会产生任何影响,但我希望在我的普通代码和优化的内部循环之间保持一致的风格......
Of course, in 99% of the cases it doesn't make a difference one way or the other, but I'd like to keep a consistent style between my normal code and my optimized inner loops...
推荐答案
与直觉相反,Hotspot 8 上最快的版本是:
Counterintuitively, the fastest version, on Hotspot 8, is:
MyClass[] arr = myList.toArray(new MyClass[0]);
我使用 jmh 运行了一个微型基准测试,结果和代码如下所示,表明带有空数组的版本始终优于带有预先设置大小的数组的版本.请注意,如果您可以重用正确大小的现有数组,结果可能会有所不同.
I have run a micro benchmark using jmh the results and code are below, showing that the version with an empty array consistently outperforms the version with a presized array. Note that if you can reuse an existing array of the correct size, the result may be different.
基准测试结果(以微秒为单位的分数,更小 = 更好):
Benchmark results (score in microseconds, smaller = better):
Benchmark (n) Mode Samples Score Error Units
c.a.p.SO29378922.preSize 1 avgt 30 0.025 ▒ 0.001 us/op
c.a.p.SO29378922.preSize 100 avgt 30 0.155 ▒ 0.004 us/op
c.a.p.SO29378922.preSize 1000 avgt 30 1.512 ▒ 0.031 us/op
c.a.p.SO29378922.preSize 5000 avgt 30 6.884 ▒ 0.130 us/op
c.a.p.SO29378922.preSize 10000 avgt 30 13.147 ▒ 0.199 us/op
c.a.p.SO29378922.preSize 100000 avgt 30 159.977 ▒ 5.292 us/op
c.a.p.SO29378922.resize 1 avgt 30 0.019 ▒ 0.000 us/op
c.a.p.SO29378922.resize 100 avgt 30 0.133 ▒ 0.003 us/op
c.a.p.SO29378922.resize 1000 avgt 30 1.075 ▒ 0.022 us/op
c.a.p.SO29378922.resize 5000 avgt 30 5.318 ▒ 0.121 us/op
c.a.p.SO29378922.resize 10000 avgt 30 10.652 ▒ 0.227 us/op
c.a.p.SO29378922.resize 100000 avgt 30 139.692 ▒ 8.957 us/op
<小时>
供参考,代码:
For reference, the code:
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
public class SO29378922 {
@Param({"1", "100", "1000", "5000", "10000", "100000"}) int n;
private final List<Integer> list = new ArrayList<>();
@Setup public void populateList() {
for (int i = 0; i < n; i++) list.add(0);
}
@Benchmark public Integer[] preSize() {
return list.toArray(new Integer[n]);
}
@Benchmark public Integer[] resize() {
return list.toArray(new Integer[0]);
}
}
<小时>
您可以在博客文章 远古智慧阵.总结一下:JVM 和 JIT 编译器包含多项优化,使其能够廉价地创建和初始化新的正确大小的数组,如果您自己创建数组,则无法使用这些优化.
You can find similar results, full analysis, and discussion in the blog post Arrays of Wisdom of the Ancients. To summarize: the JVM and JIT compiler contains several optimizations that enable it to cheaply create and initialize a new correctly sized array, and those optimizations can not be used if you create the array yourself.
这篇关于.toArray(new MyClass[0]) 还是 .toArray(new MyClass[myList.size()])?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!