Repository navigation
Expand file tree
/
Copy pathUsingMemoryMappedFile.java
More file actions
70 lines (48 loc) · 2.32 KB
/
Copy pathUsingMemoryMappedFile.java
File metadata and controls
70 lines (48 loc) · 2.32 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
package oi;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
/**
* Created by sherxon on 4/23/17.
*/
public class UsingMemoryMappedFile {
public static void main(String[] args) throws IOException {
//-------------- Test reading 1 MB file. --------------------
StopWatch.start();
RandomAccessFile memoryFile = new RandomAccessFile(DumpDataWriter.input1MB, "rw");
MappedByteBuffer mappedByteBuffer = memoryFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, DumpDataWriter.size1MB);
for (int i = 0; i < DumpDataWriter.size1MB; i++) {
mappedByteBuffer.get(i);
}
long duration = StopWatch.stop();
System.out.println(duration);
//-------------- Test reading 10 MB file. --------------------
StopWatch.start();
RandomAccessFile memoryFile1 = new RandomAccessFile(DumpDataWriter.input10MB, "rw");
MappedByteBuffer mappedByteBuffer1 = memoryFile1.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, DumpDataWriter.size10MB);
for (int i = 0; i < DumpDataWriter.size10MB; i++) {
mappedByteBuffer1.get(i);
}
long duration2 = StopWatch.stop();
System.out.println(duration2);
//-------------- Test reading 100 MB file. --------------------
StopWatch.start();
RandomAccessFile memoryFile3 = new RandomAccessFile(DumpDataWriter.input100MB, "rw");
MappedByteBuffer mappedByteBuffer3 = memoryFile3.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, DumpDataWriter.size100MB);
for (int i = 0; i < DumpDataWriter.size100MB; i++) {
mappedByteBuffer3.get(i);
}
long duration3 = StopWatch.stop();
System.out.println(duration3);
//-------------- Test reading 1000 MB file. --------------------
StopWatch.start();
RandomAccessFile memoryFile4 = new RandomAccessFile(DumpDataWriter.input1000MB, "rw");
MappedByteBuffer mappedByteBuffer4 = memoryFile4.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, DumpDataWriter.size1000MB);
for (int i = 0; i < DumpDataWriter.size1000MB; i++) {
mappedByteBuffer4.get(i);
}
long duration4 = StopWatch.stop();
System.out.println(duration4);
}
}