访问文件中的单个字符效率低? (C ++) [英] Accessing individual characters in a file inefficient? (C++)

查看:95
本文介绍了访问文件中的单个字符效率低? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为在处理文本文件时,首先将内容(或其一部分)读入std :: string或char数组,因为 - 从我的有限理解 - 读取文件从内存中的块大得多的单个字符的大小。但是,我听说现代操作系统通常不是直接从文件中读取,使我手动缓冲输入没有什么好处。

I've always assumed it to be more efficient, when processing text files, to first read the contents (or part of it) into an std::string or char array, as — from my limited understanding — files are read from memory in blocks much larger than the size of a single character. However, I've heard that modern OS's are often not actually directly reading from the file anyway, making my manually buffering the input little benefit.

说我想确定文本文件中某个字符的编号。

Say I wanted to determine the number of a certain character in a text file. Would the following be inefficient?

while (fin.get(ch)) {
    if (ch == 'n')
        ++char_count;
}



授予,我想这将取决于文件大小,

Granted, I guess it would depend on file size, but does anyone have any general rules about what's the best approach?

推荐答案

不,你的代码是高效的。文件旨在顺序读取。在幕后,保留一块RAM以便缓冲输入的数据流。事实上,因为你在整个文件被读取之前开始处理数据,你的while循环应该稍早完成。此外,您可以处理远远超出计算机主RAM的文件,而不会有任何麻烦。

No, your code is efficient. Files are intended to be read sequentially. Behind the scenes, a block of RAM is reserved in order to buffer the incoming stream of data. In fact, because you start processing data before the entire file has been read, your while loop ought to complete slightly sooner. Additionally, you can process a file far in excess of your computer's main RAM without trouble.

编辑:令我惊讶的是,Jerry的号码是。我会假设读取和解析块所获得的任何效率都会因从文件读取的成本而相形见绌。我真的想知道这个时间花在哪里,以及当文件没有缓存时,变化是多少。然而,我必须推荐杰瑞的答案这一个,特别是他指出,你真的不应该担心,直到你知道你有一个性能问题。

To my surprise, Jerry's number's pan out. I would have assumed that any efficiencies gained by reading and parsing in chunks would be dwarfed by the cost of reading from a file. I'd really like to know where that time is being spent and how much lower the variation is when the file is not cached. Nevertheless, I have to recommend Jerry's answer over this one, especially as he points out is that you really shouldn't worry about it until you know you have a performance problem.

这篇关于访问文件中的单个字符效率低? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆