|
1 | | -package com.jwetherell.algorithms; |
| 1 | +package com.jwetherell.algorithms.numbers.timing; |
2 | 2 |
|
3 | 3 | import java.text.DecimalFormat; |
4 | 4 |
|
| 5 | +import org.junit.Test; |
| 6 | + |
5 | 7 | import com.jwetherell.algorithms.numbers.Integers; |
6 | 8 | import com.jwetherell.algorithms.numbers.Longs; |
7 | 9 |
|
8 | | -public class Numbers { |
| 10 | +public class NumbersTiming { |
9 | 11 |
|
10 | 12 | private static final DecimalFormat FORMAT = new DecimalFormat("#.######"); |
11 | 13 |
|
12 | | - public static void main(String[] args) { |
| 14 | + @Test |
| 15 | + public void numbersTiming() { |
13 | 16 | // Integers |
14 | 17 | { |
15 | 18 | int a = Integer.MAX_VALUE; |
16 | 19 | System.out.println("Integer to binary string using division and modulus."); |
17 | 20 | long before = System.nanoTime(); |
18 | | - String result = Integers.toBinaryUsingDivideAndModulus(a); |
| 21 | + Integers.toBinaryUsingDivideAndModulus(a); |
19 | 22 | long after = System.nanoTime(); |
20 | | - System.out.println("a=" + a + " " + result); |
21 | 23 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
| 24 | + System.out.println(); |
22 | 25 | System.gc(); |
23 | 26 |
|
24 | 27 | System.out.println("Integer to binary string using shifts and modulus."); |
25 | 28 | before = System.nanoTime(); |
26 | | - result = Integers.toBinaryUsingShiftsAndModulus(a); |
| 29 | + Integers.toBinaryUsingShiftsAndModulus(a); |
27 | 30 | after = System.nanoTime(); |
28 | | - System.out.println("a=" + a + " " + result); |
29 | 31 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
| 32 | + System.out.println(); |
30 | 33 | System.gc(); |
31 | 34 |
|
32 | 35 | System.out.println("Integer to binary string using BigDecimal."); |
33 | 36 | before = System.nanoTime(); |
34 | | - result = Integers.toBinaryUsingBigDecimal(a); |
| 37 | + Integers.toBinaryUsingBigDecimal(a); |
35 | 38 | after = System.nanoTime(); |
36 | | - System.out.println("a=" + a + " " + result); |
37 | 39 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
| 40 | + System.out.println(); |
38 | 41 | System.gc(); |
39 | 42 |
|
40 | 43 | System.out.println("Integer to binary string using divide and double."); |
41 | 44 | before = System.nanoTime(); |
42 | | - result = Integers.toBinaryUsingDivideAndDouble(a); |
| 45 | + Integers.toBinaryUsingDivideAndDouble(a); |
43 | 46 | after = System.nanoTime(); |
44 | | - System.out.println("a=" + a + " " + result); |
45 | 47 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
| 48 | + System.out.println(); |
46 | 49 | System.gc(); |
47 | 50 |
|
48 | 51 | System.out.println("Euclid's Greatest Common Divisor."); |
49 | 52 | int x = 1989; |
50 | 53 | int y = 867; |
51 | 54 | before = System.nanoTime(); |
52 | | - int gcd = Integers.euclidsGreatestCommonDivsor(x, y); |
| 55 | + Integers.euclidsGreatestCommonDivsor(x, y); |
53 | 56 | after = System.nanoTime(); |
54 | | - System.out.println("x=" + x + " " + "y=" + y + " " + gcd); |
55 | 57 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
| 58 | + System.out.println(); |
56 | 59 | System.gc(); |
57 | 60 |
|
58 | 61 | a = (int) Math.pow(2, 30); |
59 | 62 | System.out.println("Power of 2 using loop."); |
60 | 63 | before = System.nanoTime(); |
61 | | - boolean isPowOf2 = Integers.powerOfTwoUsingLoop(a); |
| 64 | + Integers.powerOfTwoUsingLoop(a); |
62 | 65 | after = System.nanoTime(); |
63 | | - System.out.println("a=" + a + " is a power of 2? " + isPowOf2); |
64 | 66 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
| 67 | + System.out.println(); |
65 | 68 | System.gc(); |
66 | 69 |
|
67 | 70 | System.out.println("Power of 2 using recursion."); |
68 | 71 | before = System.nanoTime(); |
69 | | - isPowOf2 = Integers.powerOfTwoUsingRecursion(a); |
| 72 | + Integers.powerOfTwoUsingRecursion(a); |
70 | 73 | after = System.nanoTime(); |
71 | | - System.out.println("a=" + a + " is a power of 2? " + isPowOf2); |
72 | 74 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
| 75 | + System.out.println(); |
73 | 76 | System.gc(); |
74 | 77 |
|
75 | 78 | System.out.println("Power of 2 using logarithm."); |
76 | 79 | before = System.nanoTime(); |
77 | | - isPowOf2 = Integers.powerOfTwoUsingLog(a); |
| 80 | + Integers.powerOfTwoUsingLog(a); |
78 | 81 | after = System.nanoTime(); |
79 | | - System.out.println("a=" + a + " is a power of 2? " + isPowOf2); |
80 | 82 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
| 83 | + System.out.println(); |
81 | 84 | System.gc(); |
82 | 85 |
|
83 | 86 | System.out.println("Power of 2 using bits."); |
84 | 87 | before = System.nanoTime(); |
85 | | - isPowOf2 = Integers.powerOfTwoUsingBits(a); |
| 88 | + Integers.powerOfTwoUsingBits(a); |
86 | 89 | after = System.nanoTime(); |
87 | | - System.out.println("a=" + a + " is a power of 2? " + isPowOf2); |
88 | 90 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
89 | | - System.gc(); |
90 | | - |
91 | | - System.out.println("Integer to English"); |
92 | | - for (int i=-1001; i<=1001; i++) { |
93 | | - System.out.println(Integers.toEnglish(i)); |
94 | | - } |
95 | 91 | System.out.println(); |
| 92 | + System.gc(); |
96 | 93 | } |
97 | 94 |
|
98 | 95 | // Longs |
99 | 96 | { |
100 | 97 | long a = Long.MAX_VALUE; |
101 | 98 | System.out.println("Long to binary string using division and modulus."); |
102 | 99 | long before = System.nanoTime(); |
103 | | - String result = Longs.toBinaryUsingDivideAndModulus(a); |
| 100 | + Longs.toBinaryUsingDivideAndModulus(a); |
104 | 101 | long after = System.nanoTime(); |
105 | | - System.out.println("a=" + a + " " + result); |
106 | 102 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
| 103 | + System.out.println(); |
107 | 104 | System.gc(); |
108 | 105 |
|
109 | 106 | System.out.println("Long to binary string using shifts and modulus."); |
110 | 107 | before = System.nanoTime(); |
111 | | - result = Longs.toBinaryUsingShiftsAndModulus(a); |
| 108 | + Longs.toBinaryUsingShiftsAndModulus(a); |
112 | 109 | after = System.nanoTime(); |
113 | | - System.out.println("a=" + a + " " + result); |
114 | 110 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
| 111 | + System.out.println(); |
115 | 112 | System.gc(); |
116 | 113 |
|
117 | 114 | System.out.println("Long to binary string using BigDecimal."); |
118 | 115 | before = System.nanoTime(); |
119 | | - result = Longs.toBinaryUsingBigDecimal(a); |
| 116 | + Longs.toBinaryUsingBigDecimal(a); |
120 | 117 | after = System.nanoTime(); |
121 | | - System.out.println("a=" + a + " " + result); |
122 | 118 | System.out.println("Computed in " + FORMAT.format(after - before) + " ns"); |
| 119 | + System.out.println(); |
123 | 120 | System.gc(); |
124 | 121 | } |
125 | 122 | } |
|
0 commit comments