|
1 | 1 | package com.baeldung.numberofdigits; |
2 | 2 |
|
3 | | -import static com.baeldung.designpatterns.util.LogerUtil.LOG;; |
4 | | - |
5 | | -public class Benchmarking {; |
6 | | - |
7 | | - private static final int LOWER_BOUND = 1; |
8 | | - private static final int UPPER_BOUND = 999999999; |
9 | | - |
10 | | - |
11 | | - public static void main(String[] args) { |
12 | | - LOG.info("Testing all methods..."); |
13 | | - |
14 | | - long length = test_stringBasedSolution(); |
15 | | - LOG.info("String Based Solution : " + length); |
16 | | - |
17 | | - length = test_logarithmicApproach(); |
18 | | - LOG.info("Logarithmic Approach : " + length); |
19 | | - |
20 | | - length = test_repeatedMultiplication(); |
21 | | - LOG.info("Repeated Multiplication : " + length); |
22 | | - |
23 | | - length = test_shiftOperators(); |
24 | | - LOG.info("Shift Operators : " + length); |
25 | | - |
26 | | - length = test_dividingWithPowersOf2(); |
27 | | - LOG.info("Dividing with Powers of 2 : " + length); |
28 | | - |
29 | | - length = test_divideAndConquer(); |
30 | | - LOG.info("Divide And Conquer : " + length); |
31 | | - |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | + |
| 6 | +import org.openjdk.jmh.annotations.Benchmark; |
| 7 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 8 | +import org.openjdk.jmh.annotations.Mode; |
| 9 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 10 | +import org.openjdk.jmh.annotations.Scope; |
| 11 | +import org.openjdk.jmh.annotations.State; |
| 12 | +import org.openjdk.jmh.runner.RunnerException; |
| 13 | + |
| 14 | +public class Benchmarking { |
| 15 | + public static void main(String[] args) throws RunnerException, IOException { |
| 16 | + org.openjdk.jmh.Main.main(args); |
32 | 17 | } |
33 | 18 |
|
34 | | - private static long test_stringBasedSolution() { |
35 | | - |
36 | | - long startTime, stopTime, elapsedTime; |
37 | | - startTime = System.currentTimeMillis(); |
38 | | - |
39 | | - int total = 0; |
40 | | - for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { |
41 | | - total += NumberOfDigits.stringBasedSolution(i); |
42 | | - } |
43 | | - |
44 | | - stopTime = System.currentTimeMillis(); |
45 | | - elapsedTime = stopTime - startTime; |
46 | | - |
47 | | - return elapsedTime; |
| 19 | + @State(Scope.Thread) |
| 20 | + public static class ExecutionPlan { |
| 21 | + public int number = Integer.MAX_VALUE; |
| 22 | + public int length = 0; |
| 23 | + public NumberOfDigits numberOfDigits= new NumberOfDigits(); |
48 | 24 | } |
49 | | - |
50 | | - private static long test_logarithmicApproach() { |
51 | | - |
52 | | - long startTime, stopTime, elapsedTime; |
53 | | - startTime = System.currentTimeMillis(); |
54 | | - |
55 | | - int total = 0; |
56 | | - for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { |
57 | | - total += NumberOfDigits.logarithmicApproach(i); |
58 | | - } |
59 | | - |
60 | | - stopTime = System.currentTimeMillis(); |
61 | | - elapsedTime = stopTime - startTime; |
62 | | - |
63 | | - return elapsedTime; |
64 | | - } |
65 | | - |
66 | | - private static long test_repeatedMultiplication() { |
67 | | - |
68 | | - long startTime, stopTime, elapsedTime; |
69 | | - startTime = System.currentTimeMillis(); |
70 | | - |
71 | | - int total = 0; |
72 | | - for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { |
73 | | - total += NumberOfDigits.repeatedMultiplication(i); |
74 | | - } |
75 | | - |
76 | | - stopTime = System.currentTimeMillis(); |
77 | | - elapsedTime = stopTime - startTime; |
78 | | - |
79 | | - return elapsedTime; |
| 25 | + |
| 26 | + @Benchmark |
| 27 | + @BenchmarkMode(Mode.AverageTime) |
| 28 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 29 | + public void stringBasedSolution(ExecutionPlan plan) { |
| 30 | + plan.length = plan.numberOfDigits.stringBasedSolution(plan.number); |
80 | 31 | } |
81 | | - |
82 | | - private static long test_shiftOperators() { |
83 | | - |
84 | | - long startTime, stopTime, elapsedTime; |
85 | | - startTime = System.currentTimeMillis(); |
86 | | - |
87 | | - int total = 0; |
88 | | - for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { |
89 | | - total += NumberOfDigits.shiftOperators(i); |
90 | | - } |
91 | | - |
92 | | - stopTime = System.currentTimeMillis(); |
93 | | - elapsedTime = stopTime - startTime; |
94 | | - |
95 | | - return elapsedTime; |
| 32 | + |
| 33 | + @Benchmark |
| 34 | + @BenchmarkMode(Mode.AverageTime) |
| 35 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 36 | + public void logarithmicApproach(ExecutionPlan plan) { |
| 37 | + plan.length = plan.numberOfDigits.logarithmicApproach(plan.number); |
96 | 38 | } |
97 | | - |
98 | | - private static long test_dividingWithPowersOf2() { |
99 | | - |
100 | | - long startTime, stopTime, elapsedTime; |
101 | | - startTime = System.currentTimeMillis(); |
102 | | - |
103 | | - int total = 0; |
104 | | - for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { |
105 | | - total += NumberOfDigits.dividingWithPowersOf2(i); |
106 | | - } |
107 | | - |
108 | | - stopTime = System.currentTimeMillis(); |
109 | | - elapsedTime = stopTime - startTime; |
110 | | - |
111 | | - return elapsedTime; |
| 39 | + |
| 40 | + @Benchmark |
| 41 | + @BenchmarkMode(Mode.AverageTime) |
| 42 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 43 | + public void repeatedMultiplication(ExecutionPlan plan) { |
| 44 | + plan.length = plan.numberOfDigits.repeatedMultiplication(plan.number); |
112 | 45 | } |
113 | | - |
114 | | - private static long test_divideAndConquer() { |
115 | | - |
116 | | - long startTime, stopTime, elapsedTime; |
117 | | - startTime = System.currentTimeMillis(); |
118 | | - |
119 | | - int total = 0; |
120 | | - for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { |
121 | | - total += NumberOfDigits.divideAndConquer(i); |
122 | | - } |
123 | | - |
124 | | - stopTime = System.currentTimeMillis(); |
125 | | - elapsedTime = stopTime - startTime; |
126 | | - |
127 | | - return elapsedTime; |
| 46 | + |
| 47 | + @Benchmark |
| 48 | + @BenchmarkMode(Mode.AverageTime) |
| 49 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 50 | + public void shiftOperators(ExecutionPlan plan) { |
| 51 | + plan.length = plan.numberOfDigits.shiftOperators(plan.number); |
128 | 52 | } |
129 | 53 |
|
| 54 | + @Benchmark |
| 55 | + @BenchmarkMode(Mode.AverageTime) |
| 56 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 57 | + public void dividingWithPowersOf2(ExecutionPlan plan) { |
| 58 | + plan.length = plan.numberOfDigits.dividingWithPowersOf2(plan.number); |
| 59 | + } |
130 | 60 |
|
| 61 | + @Benchmark |
| 62 | + @BenchmarkMode(Mode.AverageTime) |
| 63 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 64 | + public void divideAndConquer(ExecutionPlan plan) { |
| 65 | + plan.length = plan.numberOfDigits.divideAndConquer(plan.number); |
| 66 | + } |
131 | 67 | } |
0 commit comments