8287903: Reduce runtime of java.math microbenchmarks

Reviewed-by: ecaspole, aph
This commit is contained in:
Claes Redestad 2022-06-09 13:11:15 +00:00
parent 3fa99844a6
commit 7e948f7ccb
4 changed files with 71 additions and 38 deletions

View file

@ -24,12 +24,15 @@ package org.openjdk.bench.java.math;
import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation; import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit; import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole; import org.openjdk.jmh.infra.Blackhole;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -40,6 +43,9 @@ import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime) @BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS) @OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread) @State(Scope.Thread)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(value = 3)
public class BigDecimals { public class BigDecimals {
/** Make sure TEST_SIZE is used to size the arrays. We need this constant to parametrize the operations count. */ /** Make sure TEST_SIZE is used to size the arrays. We need this constant to parametrize the operations count. */

View file

@ -24,6 +24,8 @@ package org.openjdk.bench.java.math;
import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation; import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit; import org.openjdk.jmh.annotations.OutputTimeUnit;
@ -31,6 +33,7 @@ import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Param; import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole; import org.openjdk.jmh.infra.Blackhole;
import java.math.BigInteger; import java.math.BigInteger;
@ -40,16 +43,16 @@ import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime) @BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS) @OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread) @State(Scope.Thread)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(value = 3)
public class BigIntegers { public class BigIntegers {
private BigInteger[] hugeArray, largeArray, smallArray, shiftArray, smallShiftArray; private BigInteger[] hugeArray, largeArray, smallArray, shiftArray;
public String[] dummyStringArray; public String[] dummyStringArray;
public Object[] dummyArr; public Object[] dummyArr;
private static final int TESTSIZE = 1000; private static final int TESTSIZE = 1000;
@Param({"32", "64", "96", "128", "160", "192", "224", "256"})
private int maxNumbits;
@Setup @Setup
public void setup() { public void setup() {
Random r = new Random(1123); Random r = new Random(1123);
@ -72,9 +75,6 @@ public class BigIntegers {
* Each array entry is atmost 16k bits * Each array entry is atmost 16k bits
* in size * in size
*/ */
smallShiftArray = new BigInteger[TESTSIZE]; /*
* Small numbers, bits count in range [maxNumbits - 31, maxNumbits]
*/
dummyStringArray = new String[TESTSIZE]; dummyStringArray = new String[TESTSIZE];
dummyArr = new Object[TESTSIZE]; dummyArr = new Object[TESTSIZE];
@ -87,7 +87,6 @@ public class BigIntegers {
largeArray[i] = new BigInteger("" + ((long) value + (long) Integer.MAX_VALUE)); largeArray[i] = new BigInteger("" + ((long) value + (long) Integer.MAX_VALUE));
smallArray[i] = new BigInteger("" + ((long) value / 1000)); smallArray[i] = new BigInteger("" + ((long) value / 1000));
shiftArray[i] = new BigInteger(numbits, r); shiftArray[i] = new BigInteger(numbits, r);
smallShiftArray[i] = new BigInteger(Math.max(maxNumbits - value % 32, 0), r);
} }
} }
@ -182,32 +181,6 @@ public class BigIntegers {
bh.consume(tmp); bh.consume(tmp);
} }
/** Invokes the shiftLeft method of small BigInteger with different values. */
@Benchmark
@OperationsPerInvocation(TESTSIZE)
public void testSmallLeftShift(Blackhole bh) {
Random rand = new Random();
int shift = rand.nextInt(30) + 1;
BigInteger tmp = null;
for (BigInteger s : smallShiftArray) {
tmp = s.shiftLeft(shift);
bh.consume(tmp);
}
}
/** Invokes the shiftRight method of small BigInteger with different values. */
@Benchmark
@OperationsPerInvocation(TESTSIZE)
public void testSmallRightShift(Blackhole bh) {
Random rand = new Random();
int shift = rand.nextInt(30) + 1;
BigInteger tmp = null;
for (BigInteger s : smallShiftArray) {
tmp = s.shiftRight(shift);
bh.consume(tmp);
}
}
/** Invokes the gcd method of BigInteger with different values. */ /** Invokes the gcd method of BigInteger with different values. */
@Benchmark @Benchmark
@OperationsPerInvocation(TESTSIZE) @OperationsPerInvocation(TESTSIZE)
@ -218,4 +191,52 @@ public class BigIntegers {
bh.consume(i2.gcd(i1)); bh.consume(i2.gcd(i1));
} }
} }
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(value = 3)
public static class SmallShifts {
@Param({"32", "128", "256"})
private int maxNumbits;
/*
* Small numbers, bits count in range [maxNumbits - 31, maxNumbits]
*/
BigInteger[] smallShiftArray = new BigInteger[TESTSIZE];
@Setup
public void setup() {
Random r = new Random(1123);
for (int i = 0; i < TESTSIZE; i++) {
int value = Math.abs(r.nextInt());
smallShiftArray[i] = new BigInteger(Math.max(maxNumbits - value % 32, 0), r);
}
}
/** Invokes the shiftLeft method of small BigInteger with different values. */
@Benchmark
@OperationsPerInvocation(TESTSIZE)
public void testLeftShift(Blackhole bh) {
Random rand = new Random();
int shift = rand.nextInt(30) + 1;
for (BigInteger s : smallShiftArray) {
bh.consume(s.shiftLeft(shift));
}
}
/** Invokes the shiftRight method of small BigInteger with different values. */
@Benchmark
@OperationsPerInvocation(TESTSIZE)
public void testRightShift(Blackhole bh) {
Random rand = new Random();
int shift = rand.nextInt(30) + 1;
for (BigInteger s : smallShiftArray) {
bh.consume(s.shiftRight(shift));
}
}
}
} }

View file

@ -29,9 +29,12 @@ import org.openjdk.jmh.annotations.*;
@OutputTimeUnit(TimeUnit.MILLISECONDS) @OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread) @State(Scope.Thread)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(value = 3)
public class FpRoundingBenchmark { public class FpRoundingBenchmark {
@Param({"1024", "2048"}) @Param({"2048"})
public int TESTSIZE; public int TESTSIZE;
public double[] DargV1; public double[] DargV1;

View file

@ -31,6 +31,9 @@ import java.util.Random;
@BenchmarkMode(Mode.AverageTime) @BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS) @OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread) @State(Scope.Thread)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(value = 2)
public class VectorSignum { public class VectorSignum {
@Param({"256", "512", "1024", "2048"}) @Param({"256", "512", "1024", "2048"})
private static int SIZE; private static int SIZE;
@ -48,7 +51,7 @@ public class VectorSignum {
floats = new float[SIZE]; floats = new float[SIZE];
res_doubles = new double[SIZE]; res_doubles = new double[SIZE];
res_floats = new float[SIZE]; res_floats = new float[SIZE];
for (int i=0; i<SIZE; i++) { for (int i = 0; i < SIZE; i++) {
floats[i] = r.nextFloat(); floats[i] = r.nextFloat();
doubles[i] = r.nextDouble(); doubles[i] = r.nextDouble();
} }
@ -56,14 +59,14 @@ public class VectorSignum {
@Benchmark @Benchmark
public void floatSignum() { public void floatSignum() {
for(int i = 0; i < SIZE; i++) { for (int i = 0; i < SIZE; i++) {
res_floats[i] = Math.signum(floats[i]); res_floats[i] = Math.signum(floats[i]);
} }
} }
@Benchmark @Benchmark
public void doubleSignum() { public void doubleSignum() {
for(int i = 0; i < SIZE; i++) { for (int i = 0; i < SIZE; i++) {
res_doubles[i] = Math.signum(doubles[i]); res_doubles[i] = Math.signum(doubles[i]);
} }
} }