有没有比 getchar() (C/C++) 更快地读取字符串的方法?
我正在参加一些编程比赛,在许多问题上都需要从输入文件中读取字符串。显然,性能是这些比赛中的一个大问题,而且字符串可能很大,所以我试图了解读取这些字符串的最有效方法。
我的猜测是,使用 getchar() 逐个字符读取字符串是最快的。这是因为即使您使用其他函数,例如 fgets() 或 getline(),这些函数仍然需要读取每个字符。
更新:我知道 I/O 不会成为大多数算法问题的瓶颈。话虽这么说,我仍然非常想知道读取字符串的最快方法是什么,如果这成为未来问题的一个问题。
I am participating in some programming competitions, and on many problems there's the need to read strings from an input file. Obviously performance is a big issue on those competitions, and strings might be huge, so I am trying to understand the most efficient way to read those strings.
My guess is that reading the strings char by char, with getchar(), is the fastest you can go. That's because even if you use other functions, say fgets() or getline(), those functions will still need to read every char anyway.
Update: I know that I/O won't be a bottleneck on most algorithmic problems. That being said I would still very much like to know what's the fastest way you can use to read strings, should this become an issue on any future problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 std::istream::read() 函数读取一大块未格式化的数据。它相对较快,正是因为数据未格式化。
operator>>
的所有重载都会读取格式化数据,这使得从流中读取数据的速度比read()
慢。同样,您可以使用 std::ostream::write() 函数一次将一大块数据写入输出流。
You can use
std::istream::read()
function to read a chunk of unformatted data. It is relatively faster precisely because the data is unformatted. All overloads ofoperator>>
read formatted data which makes reading from stream slower compared toread()
.Similarly, you can use
std::ostream::write()
function to write a chunk of data to output stream at once.反之亦然,一次性将较大的数据块读入内存比一次读取一个字符要快得多。操作系统和/或硬盘驱动器在任何情况下都可能缓存数据,但对于大型数据集,仅重复循环每个字符的标准库、操作系统、文件系统和设备驱动程序的函数调用开销就非常大。
处理字符串时,您可能会考虑一些更重要的性能问题:回到基础 作者:Joel Spolsky
不管怎样,自己回答这个问题最有说服力的方法是编写测试代码来调查不同 I/O 方法之间的差异。
The reverse is true, reading larger chunks of data into memory in one go is far faster than reading one character at a time. The OS and/or hard drive will likley cache the data in any case, but the function call overhead alone of repeatedly cycling through the standard-library, OS, file system and device driver for each character is significant for large data sets.
When handling strings there are some more important performance issues you might consider: Back to Basics by Joel Spolsky
Either way, the most convincing way to answer the question for yourself is to write test code that investigates the difference between different I/O methods.