-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpeedCrazyPattern.java
63 lines (53 loc) · 1.73 KB
/
SpeedCrazyPattern.java
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
package jp.gr.mahny_conf.replaceall_charat;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.regex.Pattern;
/**
* {@link Pattern#compile(String).replaceAll(String, String)}で制御文字を空文字に置換するよ!
* @author mahny
*
*/
public class SpeedCrazyPattern {
private static Pattern pattern = Pattern.compile("\\p{Cntrl}");
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File f = new File("resources", "test.txt");
if (!f.exists()) {
System.out.println("テストデータが無い! / file=[" + f.getAbsolutePath() + "]");
return;
}
String testData = null;
try (FileInputStream in = new FileInputStream(f);
ByteArrayOutputStream byteArrays = new ByteArrayOutputStream()) {
byte[] buff = new byte[4096];
int size = in.read(buff);
while (size != -1) {
byteArrays.write(buff, 0, size);
size = in.read(buff);
}
byteArrays.flush();
testData = new String(byteArrays.toByteArray());
}
int trials = 1000;
long startTime = System.currentTimeMillis();
for (int i = 0; i < trials; i++) {
test(testData);
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("合計時間 : " + totalTime + "(ms), 平均時間 : " + (totalTime / trials) + "(ms)");
}
/**
* 指定された文字列から制御文字を除去した文字列を生成する
* @param org 指定された文字
* @return 制御文字を除去した文字列
*/
public static String test(String org) {
return pattern.matcher(org).replaceAll("");
}
}