假设我有一个 ArrayList

ArrayList<MyClass> myList;

我想调用 toArray,是否有性能原因需要使用

MyClass[] arr = myList.toArray(new MyClass[myList.size()]);

超过

MyClass[] arr = myList.toArray(new MyClass[0]);

我更喜欢第二种风格,因为它不太冗长,而且我假设编译器将确保空数组不会真正被创建,但我一直想知道这是否属实。

当然%20在%2099%%20的%20the%20cases%20it%20doesn%27t%20make%20a%20区别%20one%20way%20or%20the%20other,%20but%20I%27d%20like%20to%20keep

答案

与直觉相反,Hotspot 8 上最快的版本是:

MyClass[] arr = myList.toArray(new MyClass[0]);

我使用 jmh 运行了一个微基准测试,结果和代码如下,显示具有空数组的版本始终优于具有预先调整大小的数组的版本。

基准测试结果(分数以微秒为单位,越小=越好):

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

作为参考,代码:

@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]);
  }
}

您可以在博客文章中找到类似的结果、完整的分析和讨论数组的古人的智慧

来自: stackoverflow.com