forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.java
More file actions
122 lines (85 loc) Β· 5.1 KB
/
Copy pathMain.java
File metadata and controls
122 lines (85 loc) Β· 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package modern.challenge;
import java.util.concurrent.TimeUnit;
public class Main {
private static final String TEXT = "My high school, the Illinois Mathematics and Science Academy, "
+ "showed me that anything is possible and that you're never too young to think big. "
+ "At 15, I worked as a computer programmer at the Fermi National Accelerator Laboratory, "
+ "or Fermilab. After graduating, I attended Stanford for a degree in economics and "
+ "computer science.";
private static final char CHAR_TO_COUNT = 'u';
private static final String TEXT_CP = "π I love π you Σ so much π π";
private static final String CHAR_TO_COUNT_CP = "Σ"; // Unicode: \u04DC, Code Point: 1244
private static final String CHAR_TO_COUNT_CPS = "π"; // Unicode: \uD83D\uDC95, Code Point: 128149
public static void main(String[] args) {
System.out.println("Input text: \n" + TEXT + "\n");
System.out.println("\n\nASCII or 16 bits Unicode characters (less than 65,535 (0xFFFF)) examples:\n");
System.out.println("replace() based solution:");
long startTimeV1 = System.nanoTime();
int countV1 = Strings
.countOccurrencesOfACertainCharacterV1(TEXT, CHAR_TO_COUNT);
displayExecutionTime(System.nanoTime() - startTimeV1);
System.out.println("Character '" + CHAR_TO_COUNT + "' occured " + countV1 + " time(s)");
System.out.println();
System.out.println("charAt() based solution:");
long startTimeV2 = System.nanoTime();
int countV2 = Strings
.countOccurrencesOfACertainCharacterV2(TEXT, CHAR_TO_COUNT);
displayExecutionTime(System.nanoTime() - startTimeV2);
System.out.println("Character '" + CHAR_TO_COUNT + "' occured " + countV2 + " time(s)");
System.out.println();
System.out.println("Java 8, functional-style solution:");
long startTimeV3 = System.nanoTime();
long countV3 = Strings
.countOccurrencesOfACertainCharacterV3(TEXT, CHAR_TO_COUNT);
displayExecutionTime(System.nanoTime() - startTimeV3);
System.out.println("Character '" + CHAR_TO_COUNT + "' occured " + countV3 + " time(s)");
System.out.println("\n--------------------------------------\n");
System.out.println("Input text: \n" + TEXT_CP + "\n");
System.out.println("\n\nIncluding Unicode surrogate pairs examples:\n");
System.out.println("replace() based solution:");
long startTimeV4 = System.nanoTime();
int countV4 = Strings
.countOccurrencesOfACertainCharacterVCP1(TEXT_CP, CHAR_TO_COUNT_CP);
displayExecutionTime(System.nanoTime() - startTimeV4);
System.out.println("Character '" + CHAR_TO_COUNT_CP + "' occured " + countV4 + " time(s)");
System.out.println();
System.out.println("replace() based solution:");
long startTimeV5 = System.nanoTime();
int countV5 = Strings
.countOccurrencesOfACertainCharacterVCP1(TEXT_CP, CHAR_TO_COUNT_CPS);
displayExecutionTime(System.nanoTime() - startTimeV5);
System.out.println("Character '" + CHAR_TO_COUNT_CPS + "' occured " + countV5 + " time(s)");
System.out.println();
System.out.println("codePointAt() based solution:");
long startTimeV6 = System.nanoTime();
int countV6 = Strings
.countOccurrencesOfACertainCharacterVCP2(TEXT_CP, CHAR_TO_COUNT_CP);
displayExecutionTime(System.nanoTime() - startTimeV6);
System.out.println("Character '" + CHAR_TO_COUNT_CP + "' occured " + countV6 + " time(s)");
System.out.println();
System.out.println("codePointAt() based solution:");
long startTimeV7 = System.nanoTime();
int countV7 = Strings
.countOccurrencesOfACertainCharacterVCP2(TEXT_CP, CHAR_TO_COUNT_CPS);
displayExecutionTime(System.nanoTime() - startTimeV7);
System.out.println("Character '" + CHAR_TO_COUNT_CPS + "' occured " + countV7 + " time(s)");
System.out.println();
System.out.println("Java 8, functional-style solution:");
long startTimeV8 = System.nanoTime();
long countV8 = Strings
.countOccurrencesOfACertainCharacterVCP3(TEXT_CP, CHAR_TO_COUNT_CP);
displayExecutionTime(System.nanoTime() - startTimeV8);
System.out.println("Character '" + CHAR_TO_COUNT_CP + "' occured " + countV8 + " time(s)");
System.out.println();
System.out.println("Java 8, functional-style solution:");
long startTimeV9 = System.nanoTime();
long countV9 = Strings
.countOccurrencesOfACertainCharacterVCP3(TEXT_CP, CHAR_TO_COUNT_CPS);
displayExecutionTime(System.nanoTime() - startTimeV9);
System.out.println("Character '" + CHAR_TO_COUNT_CPS + "' occured " + countV9 + " time(s)");
}
private static void displayExecutionTime(long time) {
System.out.println("Execution time: " + time + " ns" + " ("
+ TimeUnit.MILLISECONDS.convert(time, TimeUnit.NANOSECONDS) + " ms)");
}
}