问题描述
我认为mmap并不像使用虚拟内存那么快,它仍然具有硬盘I / O.
I think mmap is not fast as using virtual memory, it still has harddisk I/O.
但互联网上有很多人说它很快,但没有理由。
But many people on Internet say it's fast, but no reason.
在我的测试中,我使用BufferedReader和MappedByteBuffer读取文件,第一个更快。
In my test, I read a file using BufferedReader andMappedByteBuffer, the first one is faster.
推荐答案
MappedByteBuffer
适合读取不按顺序读取的二进制文件。
A MappedByteBuffer
is suitable for reading binary files that are not read sequentially.
一个示例是读取包含
树数据结构的数据库索引文件,其中文件偏移量到文件的其他部分。在这个
的情况下,你不断在文件中寻找前进和后退,并且
读取数据和
我的测试显示使用 MappedByteBuffer
比使用
RandomAccessFile
快得多。
An example is reading a database index file containingtree data-structures with file offsets to other parts of the file. In thiscase, you are continually seeking forwards and backwards in the file andreading data andmy tests showed that using a MappedByteBuffer
was much faster than using aRandomAccessFile
.
按顺序读取文本文件或二进制文件 BufferedReader
或 BufferedInputStream
效率很高。使用
a内存映射文件通常没有优势,管理内存映射
的开销可能会使 MappedByteBuffer
慢于 BufferedReader
或 BufferedInputStream
。
Reading a text file or binary file sequentially with a BufferedReader
or BufferedInputStream
is efficient. There is normally no advantage in usinga memory mapped file for this and the overhead of managing the memory mappingwill probably make a MappedByteBuffer
slower than a BufferedReader
or BufferedInputStream
.
这篇关于为什么人们说MappedByteBuffer的mmap更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!