本文介绍了是std :: ifstream明显慢于FILE吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已收到通知,我的图书馆速度比应该慢,大约30+次太慢解析特定文件(文本文件,大小326 kb)。用户建议,这可能是因为我使用 std :: ifstream (大概而不是 FILE )。 / p>

我宁愿不要盲目重写,所以我想我先检查一下,因为我的猜测将是其他地方的瓶颈。我正在读字符,所以我使用的唯一功能是 get() peek() tellg()/ seekg()



更新: >

我进行了配置,并获得了输出 - gprof didn似乎认为它花了这么长时间。我重写了程序首先将整个文件读入缓冲区,它加快了大约100x。我认为问题可能是 tellg()/ seekg()花了很长时间,但gprof可能已经无法看到由于某种原因。在任何情况下, ifstream 不会 出现以缓冲整个文件,即使对于这个大小。

解决方案

我不认为这会有所作为。特别是如果你通过char读取char,I / O的开销很可能完全支配任何 else。
为什么一次读取单个字节?



在一个326kb的文件上,最快的解决方案很可能是一次性读入内存。



std :: ifstream和C当量之间的区别基本上是一个虚函数调用或两个。如果每秒执行几千万次,它可能会有所不同,否则不会重复。文件I / O通常这么慢,以至于API用于访问它并不重要。更重要的是读/写模式。许多寻求是坏的,顺序读/写良好。


I've been informed that my library is slower than it should be, on the order of 30+ times too slow parsing a particular file (text file, size 326 kb). The user suggested that it may be that I'm using std::ifstream (presumably instead of FILE).

I'd rather not blindly rewrite, so I thought I'd check here first, since my guess would be the bottleneck is elsewhere. I'm reading character by character, so the only functions I'm using are get(), peek(), and tellg()/seekg().

Update:

I profiled, and got confusing output - gprof didn't appear to think that it took so long. I rewrote the program to read the entire file into a buffer first, and it sped up by about 100x. I think the problem may have been the tellg()/seekg() that took a long time, but gprof may have been unable to see that for some reason. In any case, ifstream does not appear to buffer the entire file, even for this size.

解决方案

I don't think that'd make a difference. Especially if you're reading char by char, the overhead of I/O is likely to completely dominate anything else.Why do you read single bytes at a time? You know how extremely inefficient it is?

On a 326kb file, the fastest solution will most likely be to just read it into memory at once.

The difference between std::ifstream and the C equivalents, is basically a virtual function call or two. It may make a difference if executed a few tens of million times per second, otherwise, not reall. file I/O is generally so slow that the API used to access it doesn't really matter. What matters far more is the read/write pattern. Lots of seeks are bad, sequential reads/writes good.

这篇关于是std :: ifstream明显慢于FILE吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:38