试图对Java 8 parallelSetAll()与setAll()进行基准测试

这个问题在这里已经有了答案:

  • 我如何在Java中编写正确的微基准测试? 11个答案

  • 使用jmh,我得到以下结果(大小是数组的长度)。 得分是每次通话的运行时间,以微秒为单位(越小越好)。

    Benchmark                     (size)  Mode  Cnt       Score       Error  Units
    SO34929316.parallelSetAll          1  avgt   20       0.077 ±     0.003  us/op
    SO34929316.parallelSetAll       1000  avgt   20       9.935 ±     0.478  us/op
    SO34929316.parallelSetAll     100000  avgt   20      56.024 ±     7.008  us/op
    SO34929316.parallelSetAll    1000000  avgt   20     518.495 ±    75.331  us/op
    SO34929316.parallelSetAll   10000000  avgt   20    5640.574 ±   139.324  us/op
    SO34929316.setAll                  1  avgt   20       0.016 ±     0.001  us/op
    SO34929316.setAll               1000  avgt   20       1.257 ±     0.023  us/op
    SO34929316.setAll             100000  avgt   20     116.760 ±     3.116  us/op
    SO34929316.setAll            1000000  avgt   20    1168.868 ±    42.153  us/op
    SO34929316.setAll           10000000  avgt   20   12347.399 ±   766.931  us/op
    

    正如你所期望的那样, parallelSetAll对于较大的数组更快,对于更小的数组则更慢。 然而,根据测试(在Win 8.1 / Xeon 2630 v2 / 6内核(x2)上运行),加速因子仅为〜x2。


    作为参考,代码:

    @State(Scope.Thread)
    @BenchmarkMode(Mode.AverageTime)
    public class SO34929316 {
    
      @Param({"1", "1000", "100000", "1000000", "10000000"}) int size;
      long[] array;
    
      @Setup(Level.Iteration)
      public void setup(){
        array = new long[size];
      }
    
      @Benchmark
      public void setAll(Blackhole bh) {
        Arrays.setAll(array, n -> n * n * 11 + n * 7);
        bh.consume(array);
      }
    
      @Benchmark
      public void parallelSetAll(Blackhole bh) {
        Arrays.parallelSetAll(array, n -> n * n * 11 + n * 7);
        bh.consume(array);
      }
    }
    
    链接地址: http://www.djcxy.com/p/86553.html

    上一篇: Trying to benchmark Java 8 parallelSetAll() vs setAll()

    下一篇: Simple Java programm runs slow on Windows 2008 Server