Standard IO efficiency

Insert picture description here

For each of these three standard I/O versions, the user CPU time is greater than the best read version in Figure 3-6, because one of the standard I/O versions that read one character at a time needs to execute 100 million In the version that reads one line at a time, there is a loop that needs to be executed 3144984 times. In the read version, the loop only needs to be executed 25224 times (for the buffer length is 4096 bytes).因为系统的CPU时间几乎相同,所以用户CPU时间的差别以及等待 I/O 结束所消耗时间的差别造成了时钟时间的差别。

  1. The system CPU time is almost the same, because all these programs have basically the same number of read and write requests to the kernel. note:

    • One advantage of using standard I/O routines is that there is no need to consider the choice of buffering and the best IO. 在使用 fgets() 时需要考虑最大行长.
  2. Known from the last column: the version implemented using getc() and putc() is roughly the same as the version of fgetc and fputc in terms of text space length.

  3. The speed of using the I/O version one line at a time is about twice the speed of the one character version at a time. If fgets and fputs are implemented with getc and putc, the expected time will be similar, and the one line version at a time is even slower. Because additional function calls need to be added. But fgets and fputs are implemented with memccpy. Usually in order to improve efficiency, the memccpy function is used 汇编语言而非C语言编写. Because of this, the one-line version at a time will have a higher speed.

  4. The last interesting point is that the fgetc version takes much less time than the buffsize == 1 in Figure 3-6. This is because system calls take more time than ordinary function calls

Guess you like

Origin blog.csdn.net/qq_22473333/article/details/114377066