-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMappedIO.java
More file actions
executable file
·63 lines (54 loc) · 1.64 KB
/
MappedIO.java
File metadata and controls
executable file
·63 lines (54 loc) · 1.64 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
package basic.nio;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
public class MappedIO {
private static int numOfInts = 4000000;
private static int numOfUbuffInts = 200000;
private abstract static class Testter {
private String name;
public Testter(String name) {
this.name = name;
}
public void runTest() {
try {
long start = System.nanoTime();
test();
double duration = System.nanoTime() - start;
System.out.format("%.2f\n", duration / 1.0e9);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public abstract void test() throws IOException;
}
private static Testter[] tests = {
new Testter("Stream Write") {
@Override
public void test() throws IOException {
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(new File("temp.tmp"))));
for (int i = 0; i < numOfInts; i++) {
dos.writeInt(1);
}
dos.close();
}
},
new Testter("Mapped Writer") {
@Override
public void test() throws IOException {
FileChannel fc = new RandomAccessFile("temp.tmp", "rw").getChannel();
IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
for (int i = 0; i < numOfInts; i++) {
ib.put(i);
}
fc.close();
}
}
};
}