版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/89047972
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
5. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/BadMicroBenchmark.java
6. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/BadMicroBenchmark2.java