简体   繁体   English

有没有办法比getchar()(C / C ++)更快地读取字符串?

[英]Is there a way to read a string faster than getchar() (C/C++)?

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. 我的猜测是,使用getchar()读取char字符串char是最快的。 That's because even if you use other functions, say fgets() or getline(), those functions will still need to read every char anyway. 那是因为即使你使用其他函数,比如fgets()或getline(),这些函数仍然需要读取每个char。

Update : I know that I/O won't be a bottleneck on most algorithmic problems. 更新 :我知道I / O不会成为大多数算法问题的瓶颈。 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. 话虽如此,我仍然非常想知道什么是你用来读取字符串的最快方法,如果这成为任何未来问题的问题。

You can use std::istream::read() function to read a chunk of unformatted data. 您可以使用std::istream::read()函数来读取一大块未格式化的数据。 It is relatively faster precisely because the data is unformatted . 它的速度相对较快,因为数据未格式化 All overloads of operator>> read formatted data which makes reading from stream slower compared to read() . operator>>所有重载operator>>读取格式化数据,与read()相比,使得从流中read()更慢。

Similarly, you can use std::ostream::write() function to write a chunk of data to output stream at once. 同样,您可以使用std::ostream::write()函数一次将一大块数据写入输出流。

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. 在任何情况下,操作系统和/或硬盘驱动器都可以缓存数据,但是单独的函数调用开销对于每个字符重复循环通过标准库,OS,文件系统和设备驱动程序对于大型数据集来说是重要的。

When handling strings there are some more important performance issues you might consider: Back to Basics by Joel Spolsky 处理字符串时,您可能会考虑一些更重要的性能问题: 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. 无论哪种方式,回答问题的最有说服力的方法是编写测试代码来研究不同I / O方法之间的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM