, , , :
echo 3 > /proc/sys/vm/drop_caches- 8 : 1000 8 ( 20 20 MiB 1 GiB).
.
1, FileChannel ByteBuffer s:
private static long method1(Path file, long dummyUsage) throws IOException, Error {
try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ)) {
for (int i = 0; i < 1000; i++) {
ByteBuffer dst = ByteBuffer.allocate(8);
if (channel.position(i * 10000).read(dst) != dst.capacity())
throw new Error("partial read");
dst.flip();
dummyUsage += dst.order(ByteOrder.LITTLE_ENDIAN).getInt();
dummyUsage += dst.order(ByteOrder.BIG_ENDIAN).getInt();
}
}
return dummyUsage;
}
:
1. 3422 ms
2. 56 ms
3. 24 ms
4. 24 ms
5. 27 ms
6. 25 ms
7. 23 ms
8. 23 ms
2, MappedByteBuffer, :
private static long method2(Path file, long dummyUsage) throws IOException {
final MappedByteBuffer buffer;
try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ)) {
buffer = channel.map(MapMode.READ_ONLY, 0L, Files.size(file));
}
for (int i = 0; i < 1000; i++) {
dummyUsage += buffer.order(ByteOrder.LITTLE_ENDIAN).getInt(i * 10000);
dummyUsage += buffer.order(ByteOrder.BIG_ENDIAN).getInt(i * 10000 + 4);
}
return dummyUsage;
}
:
1. 749 ms
2. 21 ms
3. 17 ms
4. 16 ms
5. 18 ms
6. 13 ms
7. 15 ms
8. 17 ms
3, RandomAccessFile:
private static long method3(Path file, long dummyUsage) throws IOException {
try (RandomAccessFile raf = new RandomAccessFile(file.toFile(), "r")) {
for (int i = 0; i < 1000; i++) {
raf.seek(i * 10000);
dummyUsage += Integer.reverseBytes(raf.readInt());
raf.seek(i * 10000 + 4);
dummyUsage += raf.readInt();
}
}
return dummyUsage;
}
:
1. 3479 ms
2. 104 ms
3. 81 ms
4. 84 ms
5. 78 ms
6. 81 ms
7. 81 ms
8. 81 ms
: MappedByteBuffer - - (340 140 ), , -, . , . RandomAccessFile .
: A MappedByteBuffer, , , , .