OnJava8-Examples/validating/BadMicroBenchmark2.java

36 lines
1.1 KiB
Java
Raw Normal View History

2016-08-29 07:27:53 -06:00
// validating/BadMicroBenchmark2.java
2016-01-25 18:05:55 -08:00
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-09-28 12:54:00 -06:00
// Relying on a common resource
2016-01-25 18:05:55 -08:00
import java.util.*;
import onjava.Timer;
2016-01-25 18:05:55 -08:00
public class BadMicroBenchmark2 {
2016-09-28 12:54:00 -06:00
// SIZE reduced to make it run faster:
static final int SIZE = 5_000_000;
2016-01-25 18:05:55 -08:00
public static void main(String[] args) {
long[] la = new long[SIZE];
2016-09-28 12:54:00 -06:00
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())));
2016-09-28 12:54:00 -06:00
SplittableRandom sr = new SplittableRandom();
System.out.println("parallelSetAll: " +
Timer.duration(() ->
Arrays.parallelSetAll(la, n -> sr.nextLong())));
System.out.println("setAll: " +
Timer.duration(() ->
Arrays.setAll(la, n -> sr.nextLong())));
2016-01-25 18:05:55 -08:00
}
}
/* Output:
2016-09-28 12:54:00 -06:00
parallelSetAll: 1360
setAll: 125
parallelSetAll: 75
setAll: 17
2016-01-25 18:05:55 -08:00
*/