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