Skip to content

Commit 7e948f7

Browse files
committedJun 9, 2022
8287903: Reduce runtime of java.math microbenchmarks
Reviewed-by: ecaspole, aph
1 parent 3fa9984 commit 7e948f7

File tree

4 files changed

+71
-38
lines changed

4 files changed

+71
-38
lines changed
 

‎test/micro/org/openjdk/bench/java/math/BigDecimals.java

+6
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424

2525
import org.openjdk.jmh.annotations.Benchmark;
2626
import org.openjdk.jmh.annotations.BenchmarkMode;
27+
import org.openjdk.jmh.annotations.Fork;
28+
import org.openjdk.jmh.annotations.Measurement;
2729
import org.openjdk.jmh.annotations.Mode;
2830
import org.openjdk.jmh.annotations.OperationsPerInvocation;
2931
import org.openjdk.jmh.annotations.OutputTimeUnit;
3032
import org.openjdk.jmh.annotations.Scope;
3133
import org.openjdk.jmh.annotations.Setup;
3234
import org.openjdk.jmh.annotations.State;
35+
import org.openjdk.jmh.annotations.Warmup;
3336
import org.openjdk.jmh.infra.Blackhole;
3437

3538
import java.math.BigDecimal;
@@ -40,6 +43,9 @@
4043
@BenchmarkMode(Mode.AverageTime)
4144
@OutputTimeUnit(TimeUnit.NANOSECONDS)
4245
@State(Scope.Thread)
46+
@Warmup(iterations = 5, time = 1)
47+
@Measurement(iterations = 5, time = 1)
48+
@Fork(value = 3)
4349
public class BigDecimals {
4450

4551
/** Make sure TEST_SIZE is used to size the arrays. We need this constant to parametrize the operations count. */

‎test/micro/org/openjdk/bench/java/math/BigIntegers.java

+55-34
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424

2525
import org.openjdk.jmh.annotations.Benchmark;
2626
import org.openjdk.jmh.annotations.BenchmarkMode;
27+
import org.openjdk.jmh.annotations.Fork;
28+
import org.openjdk.jmh.annotations.Measurement;
2729
import org.openjdk.jmh.annotations.Mode;
2830
import org.openjdk.jmh.annotations.OperationsPerInvocation;
2931
import org.openjdk.jmh.annotations.OutputTimeUnit;
3032
import org.openjdk.jmh.annotations.Scope;
3133
import org.openjdk.jmh.annotations.Setup;
3234
import org.openjdk.jmh.annotations.State;
3335
import org.openjdk.jmh.annotations.Param;
36+
import org.openjdk.jmh.annotations.Warmup;
3437
import org.openjdk.jmh.infra.Blackhole;
3538

3639
import java.math.BigInteger;
@@ -40,16 +43,16 @@
4043
@BenchmarkMode(Mode.AverageTime)
4144
@OutputTimeUnit(TimeUnit.NANOSECONDS)
4245
@State(Scope.Thread)
46+
@Warmup(iterations = 5, time = 1)
47+
@Measurement(iterations = 5, time = 1)
48+
@Fork(value = 3)
4349
public class BigIntegers {
4450

45-
private BigInteger[] hugeArray, largeArray, smallArray, shiftArray, smallShiftArray;
51+
private BigInteger[] hugeArray, largeArray, smallArray, shiftArray;
4652
public String[] dummyStringArray;
4753
public Object[] dummyArr;
4854
private static final int TESTSIZE = 1000;
4955

50-
@Param({"32", "64", "96", "128", "160", "192", "224", "256"})
51-
private int maxNumbits;
52-
5356
@Setup
5457
public void setup() {
5558
Random r = new Random(1123);
@@ -72,9 +75,6 @@ public void setup() {
7275
* Each array entry is atmost 16k bits
7376
* in size
7477
*/
75-
smallShiftArray = new BigInteger[TESTSIZE]; /*
76-
* Small numbers, bits count in range [maxNumbits - 31, maxNumbits]
77-
*/
7878

7979
dummyStringArray = new String[TESTSIZE];
8080
dummyArr = new Object[TESTSIZE];
@@ -87,7 +87,6 @@ public void setup() {
8787
largeArray[i] = new BigInteger("" + ((long) value + (long) Integer.MAX_VALUE));
8888
smallArray[i] = new BigInteger("" + ((long) value / 1000));
8989
shiftArray[i] = new BigInteger(numbits, r);
90-
smallShiftArray[i] = new BigInteger(Math.max(maxNumbits - value % 32, 0), r);
9190
}
9291
}
9392

@@ -182,32 +181,6 @@ public void testRightShift(Blackhole bh) {
182181
bh.consume(tmp);
183182
}
184183

185-
/** Invokes the shiftLeft method of small BigInteger with different values. */
186-
@Benchmark
187-
@OperationsPerInvocation(TESTSIZE)
188-
public void testSmallLeftShift(Blackhole bh) {
189-
Random rand = new Random();
190-
int shift = rand.nextInt(30) + 1;
191-
BigInteger tmp = null;
192-
for (BigInteger s : smallShiftArray) {
193-
tmp = s.shiftLeft(shift);
194-
bh.consume(tmp);
195-
}
196-
}
197-
198-
/** Invokes the shiftRight method of small BigInteger with different values. */
199-
@Benchmark
200-
@OperationsPerInvocation(TESTSIZE)
201-
public void testSmallRightShift(Blackhole bh) {
202-
Random rand = new Random();
203-
int shift = rand.nextInt(30) + 1;
204-
BigInteger tmp = null;
205-
for (BigInteger s : smallShiftArray) {
206-
tmp = s.shiftRight(shift);
207-
bh.consume(tmp);
208-
}
209-
}
210-
211184
/** Invokes the gcd method of BigInteger with different values. */
212185
@Benchmark
213186
@OperationsPerInvocation(TESTSIZE)
@@ -218,4 +191,52 @@ public void testGcd(Blackhole bh) {
218191
bh.consume(i2.gcd(i1));
219192
}
220193
}
194+
195+
@BenchmarkMode(Mode.AverageTime)
196+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
197+
@State(Scope.Thread)
198+
@Warmup(iterations = 5, time = 1)
199+
@Measurement(iterations = 5, time = 1)
200+
@Fork(value = 3)
201+
public static class SmallShifts {
202+
203+
@Param({"32", "128", "256"})
204+
private int maxNumbits;
205+
206+
/*
207+
* Small numbers, bits count in range [maxNumbits - 31, maxNumbits]
208+
*/
209+
BigInteger[] smallShiftArray = new BigInteger[TESTSIZE];
210+
211+
@Setup
212+
public void setup() {
213+
Random r = new Random(1123);
214+
for (int i = 0; i < TESTSIZE; i++) {
215+
int value = Math.abs(r.nextInt());
216+
smallShiftArray[i] = new BigInteger(Math.max(maxNumbits - value % 32, 0), r);
217+
}
218+
}
219+
220+
/** Invokes the shiftLeft method of small BigInteger with different values. */
221+
@Benchmark
222+
@OperationsPerInvocation(TESTSIZE)
223+
public void testLeftShift(Blackhole bh) {
224+
Random rand = new Random();
225+
int shift = rand.nextInt(30) + 1;
226+
for (BigInteger s : smallShiftArray) {
227+
bh.consume(s.shiftLeft(shift));
228+
}
229+
}
230+
231+
/** Invokes the shiftRight method of small BigInteger with different values. */
232+
@Benchmark
233+
@OperationsPerInvocation(TESTSIZE)
234+
public void testRightShift(Blackhole bh) {
235+
Random rand = new Random();
236+
int shift = rand.nextInt(30) + 1;
237+
for (BigInteger s : smallShiftArray) {
238+
bh.consume(s.shiftRight(shift));
239+
}
240+
}
241+
}
221242
}

‎test/micro/org/openjdk/bench/java/math/FpRoundingBenchmark.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929

3030
@OutputTimeUnit(TimeUnit.MILLISECONDS)
3131
@State(Scope.Thread)
32+
@Warmup(iterations = 5, time = 1)
33+
@Measurement(iterations = 5, time = 1)
34+
@Fork(value = 3)
3235
public class FpRoundingBenchmark {
3336

34-
@Param({"1024", "2048"})
37+
@Param({"2048"})
3538
public int TESTSIZE;
3639

3740
public double[] DargV1;

‎test/micro/org/openjdk/bench/java/math/VectorSignum.java ‎test/micro/org/openjdk/bench/vm/compiler/VectorSignum.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
@BenchmarkMode(Mode.AverageTime)
3232
@OutputTimeUnit(TimeUnit.NANOSECONDS)
3333
@State(Scope.Thread)
34+
@Warmup(iterations = 5, time = 1)
35+
@Measurement(iterations = 5, time = 1)
36+
@Fork(value = 2)
3437
public class VectorSignum {
3538
@Param({"256", "512", "1024", "2048"})
3639
private static int SIZE;
@@ -48,22 +51,22 @@ public void init() {
4851
floats = new float[SIZE];
4952
res_doubles = new double[SIZE];
5053
res_floats = new float[SIZE];
51-
for (int i=0; i<SIZE; i++) {
54+
for (int i = 0; i < SIZE; i++) {
5255
floats[i] = r.nextFloat();
5356
doubles[i] = r.nextDouble();
5457
}
5558
}
5659

5760
@Benchmark
5861
public void floatSignum() {
59-
for(int i = 0; i < SIZE; i++) {
62+
for (int i = 0; i < SIZE; i++) {
6063
res_floats[i] = Math.signum(floats[i]);
6164
}
6265
}
6366

6467
@Benchmark
6568
public void doubleSignum() {
66-
for(int i = 0; i < SIZE; i++) {
69+
for (int i = 0; i < SIZE; i++) {
6770
res_doubles[i] = Math.signum(doubles[i]);
6871
}
6972
}

0 commit comments

Comments
 (0)
Please sign in to comment.