Introduced jmh benchmarking (#2549)
This commit is contained in:
parent
839a68f616
commit
133bbe1aca
|
@ -201,6 +201,16 @@
|
||||||
<artifactId>vavr</artifactId>
|
<artifactId>vavr</artifactId>
|
||||||
<version>${vavr.version}</version>
|
<version>${vavr.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-core</artifactId>
|
||||||
|
<version>1.19</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-generator-annprocess</artifactId>
|
||||||
|
<version>1.19</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
178
core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java
Normal file → Executable file
178
core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java
Normal file → Executable file
|
@ -1,131 +1,67 @@
|
||||||
package com.baeldung.numberofdigits;
|
package com.baeldung.numberofdigits;
|
||||||
|
|
||||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;;
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class Benchmarking {;
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
private static final int LOWER_BOUND = 1;
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
private static final int UPPER_BOUND = 999999999;
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
|
import org.openjdk.jmh.annotations.State;
|
||||||
public static void main(String[] args) {
|
import org.openjdk.jmh.runner.RunnerException;
|
||||||
LOG.info("Testing all methods...");
|
|
||||||
|
|
||||||
long length = test_stringBasedSolution();
|
|
||||||
LOG.info("String Based Solution : " + length);
|
|
||||||
|
|
||||||
length = test_logarithmicApproach();
|
|
||||||
LOG.info("Logarithmic Approach : " + length);
|
|
||||||
|
|
||||||
length = test_repeatedMultiplication();
|
|
||||||
LOG.info("Repeated Multiplication : " + length);
|
|
||||||
|
|
||||||
length = test_shiftOperators();
|
|
||||||
LOG.info("Shift Operators : " + length);
|
|
||||||
|
|
||||||
length = test_dividingWithPowersOf2();
|
|
||||||
LOG.info("Dividing with Powers of 2 : " + length);
|
|
||||||
|
|
||||||
length = test_divideAndConquer();
|
|
||||||
LOG.info("Divide And Conquer : " + length);
|
|
||||||
|
|
||||||
|
public class Benchmarking {
|
||||||
|
public static void main(String[] args) throws RunnerException, IOException {
|
||||||
|
org.openjdk.jmh.Main.main(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long test_stringBasedSolution() {
|
@State(Scope.Thread)
|
||||||
|
public static class ExecutionPlan {
|
||||||
long startTime, stopTime, elapsedTime;
|
public int number = Integer.MAX_VALUE;
|
||||||
startTime = System.currentTimeMillis();
|
public int length = 0;
|
||||||
|
public NumberOfDigits numberOfDigits= new NumberOfDigits();
|
||||||
int total = 0;
|
|
||||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
|
||||||
total += NumberOfDigits.stringBasedSolution(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
stopTime = System.currentTimeMillis();
|
|
||||||
elapsedTime = stopTime - startTime;
|
|
||||||
|
|
||||||
return elapsedTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static long test_logarithmicApproach() {
|
|
||||||
|
|
||||||
long startTime, stopTime, elapsedTime;
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
int total = 0;
|
|
||||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
|
||||||
total += NumberOfDigits.logarithmicApproach(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
stopTime = System.currentTimeMillis();
|
|
||||||
elapsedTime = stopTime - startTime;
|
|
||||||
|
|
||||||
return elapsedTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static long test_repeatedMultiplication() {
|
|
||||||
|
|
||||||
long startTime, stopTime, elapsedTime;
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
int total = 0;
|
|
||||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
|
||||||
total += NumberOfDigits.repeatedMultiplication(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
stopTime = System.currentTimeMillis();
|
|
||||||
elapsedTime = stopTime - startTime;
|
|
||||||
|
|
||||||
return elapsedTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static long test_shiftOperators() {
|
|
||||||
|
|
||||||
long startTime, stopTime, elapsedTime;
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
int total = 0;
|
|
||||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
|
||||||
total += NumberOfDigits.shiftOperators(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
stopTime = System.currentTimeMillis();
|
|
||||||
elapsedTime = stopTime - startTime;
|
|
||||||
|
|
||||||
return elapsedTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static long test_dividingWithPowersOf2() {
|
|
||||||
|
|
||||||
long startTime, stopTime, elapsedTime;
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
int total = 0;
|
|
||||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
|
||||||
total += NumberOfDigits.dividingWithPowersOf2(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
stopTime = System.currentTimeMillis();
|
|
||||||
elapsedTime = stopTime - startTime;
|
|
||||||
|
|
||||||
return elapsedTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static long test_divideAndConquer() {
|
|
||||||
|
|
||||||
long startTime, stopTime, elapsedTime;
|
|
||||||
startTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
int total = 0;
|
|
||||||
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
|
|
||||||
total += NumberOfDigits.divideAndConquer(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
stopTime = System.currentTimeMillis();
|
|
||||||
elapsedTime = stopTime - startTime;
|
|
||||||
|
|
||||||
return elapsedTime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void stringBasedSolution(ExecutionPlan plan) {
|
||||||
|
plan.length = plan.numberOfDigits.stringBasedSolution(plan.number);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void logarithmicApproach(ExecutionPlan plan) {
|
||||||
|
plan.length = plan.numberOfDigits.logarithmicApproach(plan.number);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void repeatedMultiplication(ExecutionPlan plan) {
|
||||||
|
plan.length = plan.numberOfDigits.repeatedMultiplication(plan.number);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void shiftOperators(ExecutionPlan plan) {
|
||||||
|
plan.length = plan.numberOfDigits.shiftOperators(plan.number);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void dividingWithPowersOf2(ExecutionPlan plan) {
|
||||||
|
plan.length = plan.numberOfDigits.dividingWithPowersOf2(plan.number);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void divideAndConquer(ExecutionPlan plan) {
|
||||||
|
plan.length = plan.numberOfDigits.divideAndConquer(plan.number);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
12
core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java
Normal file → Executable file
12
core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java
Normal file → Executable file
|
@ -1,17 +1,17 @@
|
||||||
package com.baeldung.numberofdigits;
|
package com.baeldung.numberofdigits;
|
||||||
|
|
||||||
public class NumberOfDigits {
|
public class NumberOfDigits {
|
||||||
public static int stringBasedSolution(int number) {
|
public int stringBasedSolution(int number) {
|
||||||
int length = String.valueOf(number).length();
|
int length = String.valueOf(number).length();
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int logarithmicApproach(int number) {
|
public int logarithmicApproach(int number) {
|
||||||
int length = (int) Math.log10(number) + 1;
|
int length = (int) Math.log10(number) + 1;
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int repeatedMultiplication(int number) {
|
public int repeatedMultiplication(int number) {
|
||||||
int length = 0;
|
int length = 0;
|
||||||
long temp = 1;
|
long temp = 1;
|
||||||
while(temp <= number) {
|
while(temp <= number) {
|
||||||
|
@ -21,7 +21,7 @@ public class NumberOfDigits {
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int shiftOperators(int number) {
|
public int shiftOperators(int number) {
|
||||||
int length = 0;
|
int length = 0;
|
||||||
long temp = 1;
|
long temp = 1;
|
||||||
while(temp <= number) {
|
while(temp <= number) {
|
||||||
|
@ -31,7 +31,7 @@ public class NumberOfDigits {
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int dividingWithPowersOf2(int number) {
|
public int dividingWithPowersOf2(int number) {
|
||||||
int length = 1;
|
int length = 1;
|
||||||
if (number >= 100000000) {
|
if (number >= 100000000) {
|
||||||
length += 8;
|
length += 8;
|
||||||
|
@ -51,7 +51,7 @@ public class NumberOfDigits {
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int divideAndConquer(int number) {
|
public int divideAndConquer(int number) {
|
||||||
if (number < 100000){
|
if (number < 100000){
|
||||||
// 5 digits or less
|
// 5 digits or less
|
||||||
if (number < 100){
|
if (number < 100){
|
||||||
|
|
60
core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java
Normal file → Executable file
60
core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java
Normal file → Executable file
|
@ -1,27 +1,33 @@
|
||||||
package com.baeldung.numberofdigits;
|
package com.baeldung.numberofdigits;
|
||||||
|
|
||||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||||
|
|
||||||
public class NumberOfDigitsDriver {
|
public class NumberOfDigitsDriver {
|
||||||
public static void main(String[] args) {
|
private static NumberOfDigits numberOfDigits;
|
||||||
LOG.info("Testing all methods...");
|
|
||||||
|
static {
|
||||||
long length = NumberOfDigits.stringBasedSolution(602);
|
numberOfDigits = new NumberOfDigits();
|
||||||
LOG.info("String Based Solution : " + length);
|
}
|
||||||
|
|
||||||
length = NumberOfDigits.logarithmicApproach(602);
|
public static void main(String[] args) {
|
||||||
LOG.info("Logarithmic Approach : " + length);
|
LOG.info("Testing all methods...");
|
||||||
|
|
||||||
length = NumberOfDigits.repeatedMultiplication(602);
|
long length = numberOfDigits.stringBasedSolution(602);
|
||||||
LOG.info("Repeated Multiplication : " + length);
|
LOG.info("String Based Solution : " + length);
|
||||||
|
|
||||||
length = NumberOfDigits.shiftOperators(602);
|
length = numberOfDigits.logarithmicApproach(602);
|
||||||
LOG.info("Shift Operators : " + length);
|
LOG.info("Logarithmic Approach : " + length);
|
||||||
|
|
||||||
length = NumberOfDigits.dividingWithPowersOf2(602);
|
length = numberOfDigits.repeatedMultiplication(602);
|
||||||
LOG.info("Dividing with Powers of 2 : " + length);
|
LOG.info("Repeated Multiplication : " + length);
|
||||||
|
|
||||||
length = NumberOfDigits.divideAndConquer(602);
|
length = numberOfDigits.shiftOperators(602);
|
||||||
LOG.info("Divide And Conquer : " + length);
|
LOG.info("Shift Operators : " + length);
|
||||||
}
|
|
||||||
}
|
length = numberOfDigits.dividingWithPowersOf2(602);
|
||||||
|
LOG.info("Dividing with Powers of 2 : " + length);
|
||||||
|
|
||||||
|
length = numberOfDigits.divideAndConquer(602);
|
||||||
|
LOG.info("Divide And Conquer : " + length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,13 @@ import org.junit.runner.RunWith;
|
||||||
|
|
||||||
@RunWith(Theories.class)
|
@RunWith(Theories.class)
|
||||||
public class NumberOfDigitsIntegrationTest {
|
public class NumberOfDigitsIntegrationTest {
|
||||||
|
|
||||||
|
private static NumberOfDigits numberOfDigits;
|
||||||
|
|
||||||
|
static {
|
||||||
|
numberOfDigits = new NumberOfDigits();
|
||||||
|
}
|
||||||
|
|
||||||
@DataPoints
|
@DataPoints
|
||||||
public static int[][] lowestIntegers()
|
public static int[][] lowestIntegers()
|
||||||
{
|
{
|
||||||
|
@ -64,37 +70,37 @@ public class NumberOfDigitsIntegrationTest {
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenStringBasedSolutionInvoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenStringBasedSolutionInvoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], NumberOfDigits.stringBasedSolution(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.stringBasedSolution(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenLogarithmicApproachInvoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenLogarithmicApproachInvoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], NumberOfDigits.logarithmicApproach(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.logarithmicApproach(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenRepeatedMultiplicationInvoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenRepeatedMultiplicationInvoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], NumberOfDigits.repeatedMultiplication(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.repeatedMultiplication(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenShiftOperatorsInvoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenShiftOperatorsInvoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], NumberOfDigits.shiftOperators(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.shiftOperators(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenDividingWithPowersOf2Invoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenDividingWithPowersOf2Invoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], NumberOfDigits.dividingWithPowersOf2(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.dividingWithPowersOf2(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Theory
|
@Theory
|
||||||
public void givenDataPoints_whenDivideAndConquerInvoked_thenAllPointsMatch(final int[] entry) {
|
public void givenDataPoints_whenDivideAndConquerInvoked_thenAllPointsMatch(final int[] entry) {
|
||||||
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
|
||||||
Assert.assertEquals(entry[0], NumberOfDigits.divideAndConquer(entry[1]));
|
Assert.assertEquals(entry[0], numberOfDigits.divideAndConquer(entry[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue