c - 如何处理c中的大输入

标签 c

我正在解决一个网站上的练习问题,该网站指出

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/

相关文章:

c - 如何从输入数字中删除k位后得到最少的数字

无法在循环中更改数组

c - RPC 编程语言 C

c - 浮点乘法执行速度较慢,具体取决于 C 中的操作数

c - 了解 DBL_MAX

c - printf ("%d", 1.0) 是否未定义?

c - 错误: expected identifier or ‘(’ before ‘}’ token

c - 使用结构体指针的两个函数(读取和显示数组)

C winsock "rolling parsing"

我可以在 C 中分配特定数量的位数吗?