-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
92 lines (80 loc) · 3.02 KB
/
Test.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
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
package practice.countingUnicode;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.*;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
public class Test {
// test performance of map
private static void test1(Path samples) throws IOException {
Map<String, Long> result = new HashMap<>();
char[] buff = new char[2048];
StringBuilder strBuilder = new StringBuilder(2048);
// measure total time
long total_start = System.nanoTime();
try (DirectoryStream<Path> entries = Files.newDirectoryStream(samples, "*")) {
for (Path p : entries) {
// measure time for 1 file
long start = System.nanoTime();
try (FileInputStream inputStream = new FileInputStream(p.toFile());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
) {
while (reader.read(buff) != -1) {
strBuilder.append(buff);
Counter.countCharacters(strBuilder, result);
strBuilder.setLength(0);
}
// measure time for 1 file
System.out.format("time needed for %s is %d ms%n", p, (System.nanoTime() - start)/1000000);
}
}
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
// measure total time
System.out.format("Total time : %d ms%n", (System.nanoTime() - total_start)/1000000);
System.out.println(result);
}
// test performance of array
private static void test2(Path samples) throws IOException {
int[] result = new int[Character.MAX_CODE_POINT];
char[] buff = new char[2048];
// measure total time
long total_start = System.nanoTime();
try (DirectoryStream<Path> entries = Files.newDirectoryStream(samples, "*")) {
for (Path p : entries) {
// measure time for 1 file
long start = System.nanoTime();
try (FileInputStream inputStream = new FileInputStream(p.toFile());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
) {
while (reader.read(buff) != -1) {
Counter.countCharacters2(buff, result);
}
// measure time for 1 file
System.out.format("time needed for %s is %d ms%n", p, (System.nanoTime() - start)/1000000);
}
}
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
// measure total time
System.out.format("Total time : %d ms%n", (System.nanoTime() - total_start)/1000000);
for (int i = 0; i < result.length; i++) {
if (result[i] > 0) {
System.out.format("%d: %s: %d%n", i, String.valueOf(Character.toChars(i)), result[i]);
}
}
}
public static void main(String[] args) throws IOException{
Path samples = Paths.get(args[0]);
test1(samples);
}
}