validating - Microbenchmarking

Benchmarking means timing pieces of code or algorithms to see which runs faster.

// onjava/Timer.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package onjava;

import static java.util.concurrent.TimeUnit.*;

public class Timer {
  private long start = System.nanoTime();

  public long duration() {
    return NANOSECONDS.toMillis(System.nanoTime() - start);
  }

  public static long duration(Runnable test) {
    Timer timer = new Timer();
    test.run();
    return timer.duration();
  }
}
// validating/BadMicroBenchmark.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {ExcludeFromTravisCI}

import java.util.*;
import onjava.Timer;

public class BadMicroBenchmark {
  static final int SIZE = 250_000_000; // 250 million

  public static void main(String[] args) {
    try { // For machines with insufficient memory
      long[] la = new long[SIZE];
      System.out.println("setAll: " + Timer.duration(() -> Arrays.setAll(la, n -> n))); // Arrays.setAll since 1.8
      System.out.println(
          "parallelSetAll: " + Timer.duration(() -> Arrays.parallelSetAll(la, n -> n))); // Arrays.parallelSetAll since 1.8
    } catch (OutOfMemoryError e) {
      System.out.println("Insufficient memory");
      System.exit(0);
    }
  }
}
/* My Output:
setAll: 217
parallelSetAll: 204
*/

 when depends on a common resource, the parallel version can end up being much slower, as the separate contend for that resource

// validating/BadMicroBenchmark2.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Relying on a common resource

import java.util.*;
import onjava.Timer;

public class BadMicroBenchmark2 {
  // SIZE reduced to make it run faster:
  static final int SIZE = 5_000_000;

  public static void main(String[] args) {
    long[] la = new long[SIZE];
    Random r = new Random();
    System.out.println(
        "parallelSetAll: " + Timer.duration(() -> Arrays.parallelSetAll(la, n -> r.nextLong())));
    System.out.println("setAll: " + Timer.duration(() -> Arrays.setAll(la, n -> r.nextLong())));
    SplittableRandom sr = new SplittableRandom(); // since 1.8, is designed for parallel algorithms
// This algorithm was inspired by the "DotMix" algorithm
    System.out.println(
        "parallelSetAll: " + Timer.duration(() -> Arrays.parallelSetAll(la, n -> sr.nextLong())));
    System.out.println("setAll: " + Timer.duration(() -> Arrays.setAll(la, n -> sr.nextLong())));
  }
}
/* My Output:
parallelSetAll: 946
setAll: 106
parallelSetAll: 73
setAll: 16
*/

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/onjava/Timer.java

3. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/System.java

4. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/concurrent/TimeUnit.java

5. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/BadMicroBenchmark.java

6. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/BadMicroBenchmark2.java

7. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/SplittableRandom.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值