forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
35 lines (26 loc) · 1.35 KB
/
Main.java
File metadata and controls
35 lines (26 loc) · 1.35 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
package modern.challenge;
import java.time.Clock;
import java.util.concurrent.TimeUnit;
public class Main {
private static final String STRING = "111111";
private static final String SUBSTRING = "11";
public static void main(String[] args) {
Clock clock = Clock.systemUTC();
long startTimeV1 = clock.millis();
int countV1 = Strings.countStringInStringV1(STRING, SUBSTRING);
displayExecutionTime(clock.millis() - startTimeV1);
System.out.println("V1: '" + SUBSTRING + "' has occured " + countV1 + " times in '" + STRING + "'");
long startTimeV2 = clock.millis();
int countV2 = Strings.countStringInStringV2(STRING, SUBSTRING);
displayExecutionTime(clock.millis() - startTimeV2);
System.out.println("V2: '" + SUBSTRING + "' has occured " + countV2 + " times in '" + STRING + "'");
long startTimeV3 = clock.millis();
int countV3 = Strings.countStringInStringV3(STRING, SUBSTRING);
displayExecutionTime(clock.millis() - startTimeV3);
System.out.println("V3: '" + SUBSTRING + "' has occured " + countV3 + " times in '" + STRING + "'");
}
private static void displayExecutionTime(long time) {
System.out.println("Execution time: " + time + " ms" + " ("
+ TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS) + " s)");
}
}