forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced jmh benchmarking (eugenp#2549)
- Loading branch information
1 parent
839a68f
commit 133bbe1
Showing
6 changed files
with
117 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 55 additions & 119 deletions
174
core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java
100644 → 100755
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 33 additions & 27 deletions
60
core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java
100644 → 100755
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters