快速阅读文件 [英] Fast file reading
问题描述
如果我是正确的,在Linux(在 C / C ++,GCC / G ++
),可以读取使用从一个普通文件中的数据阅读(2)
或的mmap(2)
系统调用。
If I am right, on Linux (in C/C++, gcc/g++
), one can read data from a regular file using read(2)
or mmap(2)
syscalls.
两个问题。做读
系统调用使用 MMAP
内部?当首次快于第二,反之亦然?
Two questions. Do read
syscall use mmap
internally? When is first faster than the second and vice versa?
推荐答案
如果你正在阅读文件的顺序,我的默认选择是重复读
成一个相当大的缓冲区。
If you're reading the file sequentially, my default choice would be to repeatedly read
into a largish buffer.
如果您正在访问分散在一个大文件数据的小位,选择是不太清楚,但 MMAP
可能导致更多的可读性code(自就像该文件在内存中已经可以code东西)。这将提供更好的性能在这种情况下是很难说的先验。
If you're accessing small bits of data scattered around a large file, the choice is less clear, but mmap
could lead to more readable code (since you could code things up as if the file were already in memory). Which would give better performance in this case is hard to tell a priori.
如果你正在写性能关键code,然后确定性能的唯一方式是通过基准/分析实际code。
If you're writing performance-critical code, then the only way to ascertain performance is by benchmarking/profiling actual code.
这篇关于快速阅读文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!