Skip to content

Commit

Permalink
Introduced jmh benchmarking (eugenp#2549)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramansahasi authored and maibin committed Sep 4, 2017
1 parent 839a68f commit 133bbe1
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 159 deletions.
10 changes: 10 additions & 0 deletions core-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
174 changes: 55 additions & 119 deletions core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,131 +1,67 @@
package com.baeldung.numberofdigits;

import static com.baeldung.designpatterns.util.LogerUtil.LOG;;

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 java.io.IOException;
import java.util.concurrent.TimeUnit;

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;
}

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 stringBasedSolution(ExecutionPlan plan) {
plan.length = plan.numberOfDigits.stringBasedSolution(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 logarithmicApproach(ExecutionPlan plan) {
plan.length = plan.numberOfDigits.logarithmicApproach(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 repeatedMultiplication(ExecutionPlan plan) {
plan.length = plan.numberOfDigits.repeatedMultiplication(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 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 changes: 6 additions & 6 deletions core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -21,7 +21,7 @@ public static int repeatedMultiplication(int number) {
return length;
}

public static int shiftOperators(int number) {
public int shiftOperators(int number) {
int length = 0;
long temp = 1;
while(temp <= number) {
Expand All @@ -31,7 +31,7 @@ public static int shiftOperators(int number) {
return length;
}

public static int dividingWithPowersOf2(int number) {
public int dividingWithPowersOf2(int number) {
int length = 1;
if (number >= 100000000) {
length += 8;
Expand All @@ -51,7 +51,7 @@ public static int dividingWithPowersOf2(int number) {
return length;
}

public static int divideAndConquer(int number) {
public int divideAndConquer(int number) {
if (number < 100000){
// 5 digits or less
if (number < 100){
Expand Down
60 changes: 33 additions & 27 deletions core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
package com.baeldung.numberofdigits;

import static com.baeldung.designpatterns.util.LogerUtil.LOG;

public class NumberOfDigitsDriver {
public static void main(String[] args) {
LOG.info("Testing all methods...");

long length = NumberOfDigits.stringBasedSolution(602);
LOG.info("String Based Solution : " + length);

length = NumberOfDigits.logarithmicApproach(602);
LOG.info("Logarithmic Approach : " + length);

length = NumberOfDigits.repeatedMultiplication(602);
LOG.info("Repeated Multiplication : " + length);

length = NumberOfDigits.shiftOperators(602);
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);
}
}
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);
LOG.info("String Based Solution : " + length);

length = numberOfDigits.logarithmicApproach(602);
LOG.info("Logarithmic Approach : " + length);

length = numberOfDigits.repeatedMultiplication(602);
LOG.info("Repeated Multiplication : " + length);

length = numberOfDigits.shiftOperators(602);
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);
}
}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

@RunWith(Theories.class)
public class NumberOfDigitsIntegrationTest {


private static NumberOfDigits numberOfDigits;

static {
numberOfDigits = new NumberOfDigits();
}

@DataPoints
public static int[][] lowestIntegers()
{
Expand Down Expand Up @@ -64,37 +70,37 @@ public static int[][] randomIntegers()
@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]));
}

}

0 comments on commit 133bbe1

Please sign in to comment.