Skip to content

Commit b28cd2a

Browse files
committed
Improved profiling section
1 parent 7bffa3f commit b28cd2a

File tree

6 files changed

+102
-50
lines changed

6 files changed

+102
-50
lines changed

validating/BadMicroBenchmark.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.*;
66

77
public class BadMicroBenchmark {
8-
static final int SIZE = 20_000_000;
8+
static final int SIZE = 250_000_000;
99
public static void main(String[] args) {
1010
long[] la = new long[SIZE];
1111
System.out.print("setAll: ");
@@ -15,6 +15,6 @@ public static void main(String[] args) {
1515
}
1616
}
1717
/* Output:
18-
setAll: 75
19-
parallelSetAll: 110
18+
setAll: 272
19+
parallelSetAll: 301
2020
*/

validating/BadMicroBenchmark2.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,33 @@
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
5-
// Reversing the test order
5+
// Relying on a common resource
66
import java.util.*;
77

88
public class BadMicroBenchmark2 {
9-
static final int SIZE = 20_000_000;
9+
// SIZE reduced to make it run faster:
10+
static final int SIZE = 5_000_000;
1011
public static void main(String[] args) {
1112
long[] la = new long[SIZE];
13+
Random r = new Random();
1214
System.out.print("parallelSetAll: ");
13-
Time.it(() -> Arrays.parallelSetAll(la, n -> n));
15+
Time.it(() ->
16+
Arrays.parallelSetAll(la, n -> r.nextLong()));
1417
System.out.print("setAll: ");
15-
Time.it(() -> Arrays.setAll(la, n -> n));
18+
Time.it(() ->
19+
Arrays.setAll(la, n -> r.nextLong()));
20+
SplittableRandom sr = new SplittableRandom();
21+
System.out.print("parallelSetAll: ");
22+
Time.it(() ->
23+
Arrays.parallelSetAll(la, n -> sr.nextLong()));
24+
System.out.print("setAll: ");
25+
Time.it(() ->
26+
Arrays.setAll(la, n -> sr.nextLong()));
1627
}
1728
}
1829
/* Output:
19-
parallelSetAll: 68
20-
setAll: 196
30+
parallelSetAll: 1360
31+
setAll: 125
32+
parallelSetAll: 75
33+
setAll: 17
2134
*/

validating/BadMicroBenchmark3.java

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
// validating/jmh/ParallelSetAll.java
1+
// validating/jmh/JMH1.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
package validating.jmh;
66
import java.util.*;
77
import org.openjdk.jmh.annotations.*;
8+
import java.util.concurrent.TimeUnit;
89

910
@State(Scope.Thread)
10-
public class ParallelSetAll {
11+
@BenchmarkMode(Mode.AverageTime)
12+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
13+
// Increase these three for more accuracy:
14+
@Warmup(iterations = 5)
15+
@Measurement(iterations = 5)
16+
@Fork(1)
17+
public class JMH1 {
1118
private long[] la;
1219
@Setup
1320
public void setup() {
14-
la = new long[20_000_000];
21+
la = new long[250_000_000];
1522
}
1623
@Benchmark
1724
public void setAll() {
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1-
// validating/jmh/ParallelSetAllBetter.java
1+
// validating/jmh/JMH2.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
package validating.jmh;
66
import java.util.*;
77
import org.openjdk.jmh.annotations.*;
8+
import java.util.concurrent.TimeUnit;
89

910
@State(Scope.Thread)
10-
public class ParallelSetAllBetter {
11+
@BenchmarkMode(Mode.AverageTime)
12+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
13+
@Warmup(iterations = 5)
14+
@Measurement(iterations = 5)
15+
@Fork(1)
16+
public class JMH2 {
1117
private long[] la;
12-
@Param({"1", "100", "10000", "1000000", "20000000"})
13-
int count;
18+
@Param({
19+
"1",
20+
"10",
21+
"100",
22+
"1000",
23+
"10000",
24+
"100000",
25+
"1000000",
26+
"10000000",
27+
"100000000",
28+
"250000000"
29+
})
30+
int size;
1431

1532
@Setup
1633
public void setup() {
17-
la = new long[count];
34+
la = new long[size];
1835
}
1936
@Benchmark
2037
public void setAll() {

validating/jmh/JMH3.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// validating/jmh/JMH3.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
package validating.jmh;
6+
import java.util.*;
7+
import org.openjdk.jmh.annotations.*;
8+
import java.util.concurrent.TimeUnit;
9+
10+
@State(Scope.Thread)
11+
@BenchmarkMode(Mode.AverageTime)
12+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
13+
@Warmup(iterations = 5)
14+
@Measurement(iterations = 5)
15+
@Fork(1)
16+
public class JMH3 {
17+
private long[] la;
18+
@Param({
19+
"1",
20+
"10",
21+
"100",
22+
"1000",
23+
"10000",
24+
"100000",
25+
"1000000",
26+
"10000000",
27+
"100000000",
28+
"250000000"
29+
})
30+
int size;
31+
32+
@Setup
33+
public void setup() {
34+
la = new long[size];
35+
}
36+
public static long f(long x) {
37+
long divisor = x % 25 + 1;
38+
return Long.divideUnsigned(x, divisor);
39+
}
40+
@Benchmark
41+
public void setAll() {
42+
Arrays.setAll(la, n -> f(n));
43+
}
44+
@Benchmark
45+
public void parallelSetAll() {
46+
Arrays.parallelSetAll(la, n -> f(n));
47+
}
48+
}

0 commit comments

Comments
 (0)