-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMatch.java
More file actions
38 lines (31 loc) Β· 1.25 KB
/
Copy pathMatch.java
File metadata and controls
38 lines (31 loc) Β· 1.25 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
package Chapter2.Day6;
import java.util.regex.Pattern;
public class Match {
private static final Pattern ROMAN = Pattern.compile("^(?=[MDCLXVI])M*D?C{0,4}L?X{0,4}V?I{0,4}$");
public static void main(String[] args) {
long before = System.currentTimeMillis();
String s = "hi";
for (int i = 0; i < 20000000; i++) {
isRoman(s);
}
System.out.println("μ κ·μ λ°λ³΅ μμ± : " + (System.currentTimeMillis() - before));
before = System.currentTimeMillis();
for (int i = 0; i < 20000000; i++) {
isRomanRefactor(s);
}
System.out.println("μ κ·μ μμ μ¬μ© : " + (System.currentTimeMillis() - before)); // 10λ°° λΉ λ¦
}
/**
* matches λ©μλλ λ΄λΆμ μΌλ‘ μ κ·μμ κ°μ§κ³ Pattern κ°μ²΄λ₯Ό λ§λ€μ΄ λΉκ΅νλ€.
* μλμ λ©μλλ₯Ό λ°λ³΅νμ¬ μ€ννλ©΄, κ°μ μ κ·μμ κ°μ§κ³ κ°μ κ°μ²΄λ₯Ό μ¬λ¬λ² μ€ννλ€.
*/
private static boolean isRoman(String s) {
return s.matches("^(?=[MDCLXVI])M*D?C{0,4}L?X{0,4}V?I{0,4}$");
}
/**
* μμλ₯Ό μ΄μ©ν 리νν λ§ λ²μ
*/
private static boolean isRomanRefactor(String s) {
return ROMAN.matcher(s).matches();
}
}