我正在解决一个网站上的练习问题,该网站指出
The purpose of this problem is to verify whether the method you are using to read input data is sufficiently fast to handle problems branded with the enormous Input/Output warning. You are expected to be able to process at least 2.5MB of input data per second at runtime.
此外,如何优化 printf 和 scanf 之外的输入/输出例程?
最佳答案
它是特定于操作系统的(因为 C 标准只知道 <stdio.h>
)。对于 Linux,请考虑使用低级系统调用来提高效率,例如 open(2) , mmap(2) , read(2) , pread(2) , write(2) 。您可能还想使用 readahead(2) 。不要忘记在相当大的 block (例如 128Kbytes)中进行 I/O,如果可能的话进行页面对齐。阅读Advanced Linux Programming书。
如果仅限于标准 C99 函数,请使用 fread(3)在相当大的 block 上。还可以考虑使用 setvbuf(3) 增加内部缓冲区
2.5Mbyte/秒并不是很令人印象深刻。瓶颈可能是硬件,但在标准桌面硬件上您应该能够达到 20 或 50Mbytes/sec。使用SSD会有很大帮助。
关于c - 如何处理c中的大输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13093633/