-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMappedIO.java
More file actions
139 lines (116 loc) · 4.07 KB
/
MappedIO.java
File metadata and controls
139 lines (116 loc) · 4.07 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package io;
import java.nio.*;
import java.nio.channels.*;
import java.io.*;
import java.util.*;
/**
* RUN:
* javac io/MappedIO.java && java io.MappedIO
*
* OUTPUT:
* Stream Write: 0,809347
* Mapped Write: 0,065621
* Stream Read: 0,482614
* Mapped Read: 0,034600
* Stream Read/Write: 1,765893
* Mapped Read/Write: 0,005478
*/
public class MappedIO {
private static int numOfInts = 4000000;
private static int numOfUbuffInts = 200000;
private abstract static class Tester {
private String name;
public Tester(String name) { this.name = name;}
public void runTest() {
System.out.print(name +": ");
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 Tester[] tests = {
new Tester("Stream Write") {
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(i);
}
dos.close();
}
},
new Tester("Mapped Write") {
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();
}
},
new Tester("Stream Read") {
public void test() throws IOException {
DataInputStream dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream("temp.tmp")
)
);
for (int i = 0; i < numOfInts; i++) {
dis.readInt();
}
dis.close();
}
},
new Tester("Mapped Read") {
public void test() throws IOException {
FileChannel fc = new FileInputStream(new File("temp.tmp")).getChannel();
// FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "r").getChannel();
IntBuffer ib = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer();
while(ib.hasRemaining()) {
ib.get();
}
fc.close();
}
},
new Tester("Stream Read/Write") {
public void test() throws IOException {
RandomAccessFile raf = new RandomAccessFile(new File("temp.tmp"), "rw");
raf.writeInt(1);
for (int i = 0; i < numOfUbuffInts; i++) {
raf.seek(raf.length()-4);
raf.writeInt(raf.readInt());
}
raf.close();
}
},
new Tester("Mapped Read/Write") {
public void test() throws IOException {
FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
ib.put(0);
for (int i = 1; i < numOfUbuffInts; i++) {
ib.put(ib.get(i-1));
}
fc.close();
}
},
};
public static void main(String[] args) {
for (Tester test : tests) {
test.runTest();
}
}
}