2016-08-29 07:27:53 -06:00
|
|
|
// validating/BadMicroBenchmark2.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2016-01-25 18:05:55 -08:00
|
|
|
// 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.*;
|
2016-12-17 10:51:25 -08:00
|
|
|
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();
|
2016-12-17 10:51:25 -08:00
|
|
|
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();
|
2016-12-17 10:51:25 -08:00
|
|
|
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:
|
2017-05-10 11:45:39 -06:00
|
|
|
parallelSetAll: 1147
|
|
|
|
setAll: 174
|
|
|
|
parallelSetAll: 86
|
|
|
|
setAll: 39
|
2016-01-25 18:05:55 -08:00
|
|
|
*/
|