C ++程序员应该经常使用std :: flush吗? [重复]

时间:2021-10-04 18:52:42

This question already has an answer here:

这个问题在这里已有答案:

Is it recommended that C++ programmers frequently write lines like

是否建议C ++程序员经常编写类似的行

std::cout << "output: " << i << " and " << j << std::flush;
//more
std::cout << "ending newline." << std::endl; //endl does flush

In other words, in output lines that don't have endl, should we be flushing alot, just in case? Or is this not really needed anymore these days on most platforms?

换句话说,在没有endl的输出行中,我们是否应该进行大量冲洗,以防万一?或者这些日子在大多数平台上不再需要了吗?

2 个解决方案

#1


16  

Your average program does not require frequent flushing. Flushing is something nearer to a special case needed in a few situations:

您的平均课程不需要经常冲洗。在某些情况下,Flushing是一种特殊情况:

  • Interacting with a human or other system: flushing output before waiting for input is sensible.
  • 与人或其他系统交互:在等待输入之前刷新输出是明智的。

  • Going dormant for awhile: Flushing before extended sleep or waiting simplifies examination of logfiles, makes databases consistent most of the time, etc.
  • 休眠一段时间:在长时间睡眠或等待之前进行刷新简化了日志文件的检查,使数据库在大多数时间保持一致,等等。

If buffering is not needed, it would be better to disable buffering in the first place instead of throwing in a lot of flushes.

如果不需要缓冲,最好先禁用缓冲,而不是投入大量的冲洗。

Most of the time, programs benefit by having buffering enabled. Sometimes they generate a few characters here and there. Other times they output a blast of lines.

大多数情况下,程序通过启用缓冲而受益。有时他们会在这里和那里产生一些角色。其他时候,他们输出一线。

In all my decades of engineering, my most dramatic performance increases are often realized simply by improving buffering. Sometimes by increasing the default FILE buffer size above 512 bytes (the default) to 4K or 32K (sometimes higher). Other times by adding a layer of buffering or caching. Usually there is high overhead with each trip through the operating system's i/o system. Reducing the total number of system calls is (usually) an easy and highly effective scheme to improve performance.

在我几十年的工程设计中,我最显着的性能提升通常只是通过改进缓冲来实现。有时通过将默认的FILE缓冲区大小增加到512字节(默认值)到4K或32K(有时更高)。其他时候通过添加一层缓冲或缓存。通常,每次通过操作系统的i / o系统都会产生很高的开销。减少系统调用的总数(通常)是一种简单而高效的方案,可以提高性能。

#2


14  

Flushing is generally not the greatest habit to get into, as flushing can slow down your program at times if you're constantly writing out to IO. You can control how you flush by either explicitly using std::endl or std::flush (with std::endl just inserting a \n into a stream and then calling flush).

冲洗通常不是最常进入的习惯,因为如果你不断写出IO,冲洗可能会减慢你的程序。您可以通过显式使用std :: endl或std :: flush来控制刷新方式(使用std :: endl只需将\ n插入流中然后调用flush)。

@StackedCrooked in the C++ Lounge put together an experiment about the cost of flushing versus not flushing at all: http://coliru.stacked-crooked.com/view?id=55c830cf9a144559f31963de41fa9405-f674c1a6d04c632b71a62362c0ccfc51

C ++ Lounge中的@StackedCrooked汇总了一个关于冲洗成本而非冲洗成本的实验:http://coliru.stacked-crooked.com/view?id = 55c830cf9a144559f31963de41fa9405-f674c1a6d04c632b71a62362c0ccfc51

Not-flushing does relatively well after repeated use, while flushing adds a bit of overhead everytime you invoke it: you are honestly better off not manually std::flush-ing your streams. Just do it once at the end of the program or after the end of a critical section of code.

重复使用后,非刷新功能相对较好,而每次调用时刷新都会增加一些开销:老实说,最好不要手动std :: flush-ing你的流。只需在程序结束时或在关键代码段结束后执行一次。

It's also good to note that you should probably flush just before you do anything with the user, so the program isn't not-writing things to the output that a user should be seeing in a Log File or other place.

同样值得注意的是,您应该在对用户执行任何操作之前进行刷新,因此程序不会将内容写入用户应在日志文件或其他位置看到的输出。

EDIT: Relevant analogy: In simple terms, what is the purpose of flush() in ostream

编辑:相关类比:简单来说,ostream中flush()的目的是什么

#1


16  

Your average program does not require frequent flushing. Flushing is something nearer to a special case needed in a few situations:

您的平均课程不需要经常冲洗。在某些情况下,Flushing是一种特殊情况:

  • Interacting with a human or other system: flushing output before waiting for input is sensible.
  • 与人或其他系统交互:在等待输入之前刷新输出是明智的。

  • Going dormant for awhile: Flushing before extended sleep or waiting simplifies examination of logfiles, makes databases consistent most of the time, etc.
  • 休眠一段时间:在长时间睡眠或等待之前进行刷新简化了日志文件的检查,使数据库在大多数时间保持一致,等等。

If buffering is not needed, it would be better to disable buffering in the first place instead of throwing in a lot of flushes.

如果不需要缓冲,最好先禁用缓冲,而不是投入大量的冲洗。

Most of the time, programs benefit by having buffering enabled. Sometimes they generate a few characters here and there. Other times they output a blast of lines.

大多数情况下,程序通过启用缓冲而受益。有时他们会在这里和那里产生一些角色。其他时候,他们输出一线。

In all my decades of engineering, my most dramatic performance increases are often realized simply by improving buffering. Sometimes by increasing the default FILE buffer size above 512 bytes (the default) to 4K or 32K (sometimes higher). Other times by adding a layer of buffering or caching. Usually there is high overhead with each trip through the operating system's i/o system. Reducing the total number of system calls is (usually) an easy and highly effective scheme to improve performance.

在我几十年的工程设计中,我最显着的性能提升通常只是通过改进缓冲来实现。有时通过将默认的FILE缓冲区大小增加到512字节(默认值)到4K或32K(有时更高)。其他时候通过添加一层缓冲或缓存。通常,每次通过操作系统的i / o系统都会产生很高的开销。减少系统调用的总数(通常)是一种简单而高效的方案,可以提高性能。

#2


14  

Flushing is generally not the greatest habit to get into, as flushing can slow down your program at times if you're constantly writing out to IO. You can control how you flush by either explicitly using std::endl or std::flush (with std::endl just inserting a \n into a stream and then calling flush).

冲洗通常不是最常进入的习惯,因为如果你不断写出IO,冲洗可能会减慢你的程序。您可以通过显式使用std :: endl或std :: flush来控制刷新方式(使用std :: endl只需将\ n插入流中然后调用flush)。

@StackedCrooked in the C++ Lounge put together an experiment about the cost of flushing versus not flushing at all: http://coliru.stacked-crooked.com/view?id=55c830cf9a144559f31963de41fa9405-f674c1a6d04c632b71a62362c0ccfc51

C ++ Lounge中的@StackedCrooked汇总了一个关于冲洗成本而非冲洗成本的实验:http://coliru.stacked-crooked.com/view?id = 55c830cf9a144559f31963de41fa9405-f674c1a6d04c632b71a62362c0ccfc51

Not-flushing does relatively well after repeated use, while flushing adds a bit of overhead everytime you invoke it: you are honestly better off not manually std::flush-ing your streams. Just do it once at the end of the program or after the end of a critical section of code.

重复使用后,非刷新功能相对较好,而每次调用时刷新都会增加一些开销:老实说,最好不要手动std :: flush-ing你的流。只需在程序结束时或在关键代码段结束后执行一次。

It's also good to note that you should probably flush just before you do anything with the user, so the program isn't not-writing things to the output that a user should be seeing in a Log File or other place.

同样值得注意的是,您应该在对用户执行任何操作之前进行刷新,因此程序不会将内容写入用户应在日志文件或其他位置看到的输出。

EDIT: Relevant analogy: In simple terms, what is the purpose of flush() in ostream

编辑:相关类比:简单来说,ostream中flush()的目的是什么