Improved profiling section

This commit is contained in:
Bruce Eckel 2016-09-28 12:54:00 -06:00
parent 7bffa3f30d
commit b28cd2a3f4
6 changed files with 102 additions and 50 deletions

View File

@ -5,7 +5,7 @@
import java.util.*; import java.util.*;
public class BadMicroBenchmark { public class BadMicroBenchmark {
static final int SIZE = 20_000_000; static final int SIZE = 250_000_000;
public static void main(String[] args) { public static void main(String[] args) {
long[] la = new long[SIZE]; long[] la = new long[SIZE];
System.out.print("setAll: "); System.out.print("setAll: ");
@ -15,6 +15,6 @@ public class BadMicroBenchmark {
} }
} }
/* Output: /* Output:
setAll: 75 setAll: 272
parallelSetAll: 110 parallelSetAll: 301
*/ */

View File

@ -2,20 +2,33 @@
// (c)2016 MindView LLC: see Copyright.txt // (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Reversing the test order // Relying on a common resource
import java.util.*; import java.util.*;
public class BadMicroBenchmark2 { public class BadMicroBenchmark2 {
static final int SIZE = 20_000_000; // SIZE reduced to make it run faster:
static final int SIZE = 5_000_000;
public static void main(String[] args) { public static void main(String[] args) {
long[] la = new long[SIZE]; long[] la = new long[SIZE];
Random r = new Random();
System.out.print("parallelSetAll: "); System.out.print("parallelSetAll: ");
Time.it(() -> Arrays.parallelSetAll(la, n -> n)); Time.it(() ->
Arrays.parallelSetAll(la, n -> r.nextLong()));
System.out.print("setAll: "); System.out.print("setAll: ");
Time.it(() -> Arrays.setAll(la, n -> n)); Time.it(() ->
Arrays.setAll(la, n -> r.nextLong()));
SplittableRandom sr = new SplittableRandom();
System.out.print("parallelSetAll: ");
Time.it(() ->
Arrays.parallelSetAll(la, n -> sr.nextLong()));
System.out.print("setAll: ");
Time.it(() ->
Arrays.setAll(la, n -> sr.nextLong()));
} }
} }
/* Output: /* Output:
parallelSetAll: 68 parallelSetAll: 1360
setAll: 196 setAll: 125
parallelSetAll: 75
setAll: 17
*/ */

View File

@ -1,33 +0,0 @@
// validating/BadMicroBenchmark3.java
// (c)2016 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.*;
public class BadMicroBenchmark3 {
static final int SIZE = 20_000_000;
public static void main(String[] args) {
long[] la = new long[SIZE];
Random r = new Random();
System.out.print("parallelSetAll: ");
Time.it(() ->
Arrays.parallelSetAll(la, n -> r.nextLong()));
System.out.print("setAll: ");
Time.it(() ->
Arrays.setAll(la, n -> r.nextLong()));
SplittableRandom sr = new SplittableRandom();
System.out.print("parallelSetAll: ");
Time.it(() ->
Arrays.parallelSetAll(la, n -> sr.nextLong()));
System.out.print("setAll: ");
Time.it(() ->
Arrays.setAll(la, n -> sr.nextLong()));
}
}
/* Output:
parallelSetAll: 4540
setAll: 1540
parallelSetAll: 398
setAll: 739
*/

View File

@ -1,17 +1,24 @@
// validating/jmh/ParallelSetAll.java // validating/jmh/JMH1.java
// (c)2016 MindView LLC: see Copyright.txt // (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
package validating.jmh; package validating.jmh;
import java.util.*; import java.util.*;
import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
@State(Scope.Thread) @State(Scope.Thread)
public class ParallelSetAll { @BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
// Increase these three for more accuracy:
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@Fork(1)
public class JMH1 {
private long[] la; private long[] la;
@Setup @Setup
public void setup() { public void setup() {
la = new long[20_000_000]; la = new long[250_000_000];
} }
@Benchmark @Benchmark
public void setAll() { public void setAll() {

View File

@ -1,20 +1,37 @@
// validating/jmh/ParallelSetAllBetter.java // validating/jmh/JMH2.java
// (c)2016 MindView LLC: see Copyright.txt // (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
package validating.jmh; package validating.jmh;
import java.util.*; import java.util.*;
import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
@State(Scope.Thread) @State(Scope.Thread)
public class ParallelSetAllBetter { @BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@Fork(1)
public class JMH2 {
private long[] la; private long[] la;
@Param({"1", "100", "10000", "1000000", "20000000"}) @Param({
int count; "1",
"10",
"100",
"1000",
"10000",
"100000",
"1000000",
"10000000",
"100000000",
"250000000"
})
int size;
@Setup @Setup
public void setup() { public void setup() {
la = new long[count]; la = new long[size];
} }
@Benchmark @Benchmark
public void setAll() { public void setAll() {

48
validating/jmh/JMH3.java Normal file
View File

@ -0,0 +1,48 @@
// validating/jmh/JMH3.java
// (c)2016 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 validating.jmh;
import java.util.*;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@Fork(1)
public class JMH3 {
private long[] la;
@Param({
"1",
"10",
"100",
"1000",
"10000",
"100000",
"1000000",
"10000000",
"100000000",
"250000000"
})
int size;
@Setup
public void setup() {
la = new long[size];
}
public static long f(long x) {
long divisor = x % 25 + 1;
return Long.divideUnsigned(x, divisor);
}
@Benchmark
public void setAll() {
Arrays.setAll(la, n -> f(n));
}
@Benchmark
public void parallelSetAll() {
Arrays.parallelSetAll(la, n -> f(n));
}
}