diff --git a/core-java/pom.xml b/core-java/pom.xml
index 3805506181..d4d249934b 100644
--- a/core-java/pom.xml
+++ b/core-java/pom.xml
@@ -201,6 +201,16 @@
vavr
${vavr.version}
+
+ org.openjdk.jmh
+ jmh-core
+ 1.19
+
+
+ org.openjdk.jmh
+ jmh-generator-annprocess
+ 1.19
+
diff --git a/core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java b/core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java
old mode 100644
new mode 100755
index fbd9af2c30..737654ccf5
--- a/core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java
+++ b/core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java
@@ -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;
- }
-
- 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;
+ @State(Scope.Thread)
+ public static class ExecutionPlan {
+ public int number = Integer.MAX_VALUE;
+ public int length = 0;
+ public NumberOfDigits numberOfDigits= new NumberOfDigits();
}
+ @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);
+ }
}
diff --git a/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java b/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java
old mode 100644
new mode 100755
index f455430750..1abf74d405
--- a/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java
+++ b/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java
@@ -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){
diff --git a/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java b/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java
old mode 100644
new mode 100755
index 4d76a5d677..32d3051327
--- a/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java
+++ b/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java
@@ -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);
+ }
+}
diff --git a/core-java/src/main/resources/META-INF/BenchmarkList b/core-java/src/main/resources/META-INF/BenchmarkList
new file mode 100755
index 0000000000..e69de29bb2
diff --git a/core-java/src/test/java/com/baeldung/numberofdigits/NumberOfDigitsIntegrationTest.java b/core-java/src/test/java/com/baeldung/numberofdigits/NumberOfDigitsIntegrationTest.java
index 01b874622e..b348fe01ef 100644
--- a/core-java/src/test/java/com/baeldung/numberofdigits/NumberOfDigitsIntegrationTest.java
+++ b/core-java/src/test/java/com/baeldung/numberofdigits/NumberOfDigitsIntegrationTest.java
@@ -9,7 +9,13 @@ 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]));
}
}
\ No newline at end of file