二进制文件的 ifstream 与 fread
哪个更快? ifstream
或 fread
。
我应该使用哪个来读取二进制文件?
fread()
将整个文件放入内存。
因此,在 fread
之后,访问它创建的缓冲区速度很快。
ifstream::open()
是否将整个文件放入内存?
或者每次我们运行 ifstream::read() 时它都会访问硬盘吗?
那么... ifstream::open() == fread()
吗?
或者 (ifstream::open(); ifstream::read(file_length);
) == fread()
?
或者我应该使用 ifstream::rdbuf()->read() 吗?
编辑: 我的 readFile() 方法现在看起来像这样:
void readFile()
{
std::ifstream fin;
fin.open("largefile.dat", ifstream::binary | ifstream::in);
// in each of these small read methods, there are at least 1 fin.read()
// call inside.
readHeaderInfo(fin);
readPreference(fin);
readMainContent(fin);
readVolumeData(fin);
readTextureData(fin);
fin.close();
}
小方法中的多个 fin.read() 调用会减慢程序速度吗? 我应该在 main 方法中只使用 1 个 fin.read() 并将缓冲区传递给小方法吗?我想我要写一个小程序来测试一下。
谢谢!
Which is faster? ifstream
or fread
.
Which should I use to read binary files?
fread()
puts the whole file into the memory.
So after fread
, accessing the buffer it creates is fast.
Does ifstream::open()
puts the whole file into the memory?
or does it access the hard disk every time we run ifstream::read()
?
So... does ifstream::open()
== fread()
?
or (ifstream::open(); ifstream::read(file_length);
) == fread()
?
Or shall I use ifstream::rdbuf()->read()
?
edit:
My readFile() method now looks something like this:
void readFile()
{
std::ifstream fin;
fin.open("largefile.dat", ifstream::binary | ifstream::in);
// in each of these small read methods, there are at least 1 fin.read()
// call inside.
readHeaderInfo(fin);
readPreference(fin);
readMainContent(fin);
readVolumeData(fin);
readTextureData(fin);
fin.close();
}
Will the multiple fin.read() calls in the small methods slow down the program?
Shall I only use 1 fin.read() in the main method and pass the buffer into the small methods? I guess I am going to write a small program to test.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您真的确定
fread
将整个文件放入内存吗?文件访问可以被缓冲,但我怀疑你是否真的将整个文件放入内存中。我认为ifstream::read
只是以更符合 C++ 的方式在底层使用fread
(因此是从 C++ 文件中读取二进制信息的标准方法)。我怀疑是否存在显着的性能差异。要使用
fread
,文件必须打开。它不仅仅需要一个文件并将其立即放入内存中。所以ifstream::open == fopen
和ifstream::read == fread
。Are you really sure about
fread
putting the whole file into memory? File access can be buffered, but I doubt that you really get the whole file put into memory. I thinkifstream::read
just usesfread
under the hood in a more C++ conformant way (and is therefore the standard way of reading binary information from a file in C++). I doubt that there is a significant performance difference.To use
fread
, the file has to be open. It doesn't take just a file and put it into memory at once. soifstream::open == fopen
andifstream::read == fread
.如果您使用高级 api,C++ 流 api 通常比 C 文件 api 慢一点,但它提供比 C 更干净/更安全的 api。
如果您想要速度,请考虑使用内存映射文件,尽管标准库没有可移植的方法来执行此操作。
C++ stream api is usually a little bit slower then C file api if you use high level api, but it provides cleaner/safer api then C.
If you want speed, consider using memory mapped files, though there is no portable way of doing this with standard library.
至于哪个更快,请看我的评论。对于其余的:
ifstream
来说,我确信 IO 是缓冲的,因此每次读取时不一定都会进行磁盘访问。As to which is faster, see my comment. For the rest:
ifstream
I am sure that the IO is buffered, so there will not necessarily be a disk access for every read you make.C++ 文件流的想法是,部分或全部文件缓冲在内存中(基于它认为是最佳的),并且您不必担心它。
我会使用 ifstream::read() 并告诉它您需要多少。
The idea with C++ file streams is that some or all of the file is buffered in memory (based on what it thinks is optimal) and that you don't have to worry about it.
I would use
ifstream::read()
and just tell it how much you need.使用流运算符:
Use stream operator: