2

I am writing some console programs and I notice that sometimes when I use print() and my program is idle, not everything is printed out (the last few lines are missing).

Eventually something will happen and the lines do get printed, but often when I close the program the last few lines are not there.

So I did some digging and it looks like the stdout buffer is not always emptied unless certain conditions are met (new line? / line feed?).

so I have created a "myprintf()" function which wraps printf to do the following (in pseudo code):

printf(...);
fflush(stdout);

The question is, apart from the obvious extra function call overhead, is this going to slow my program down? I.e. is this a bad practice performance wise?

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • Usually, the answer to this question is... MEASURE THAT! By the way, I guess that if your program does not have long computations (in the order of seconds), then the flushing will be a big percentage of the total work, but you will not notice it, because it will be just a couple milliseconds. – lodo Nov 18 '15 at 09:07
  • 1
    For C++ there are many similar questions related to this topic, about using `std::endl` (which adds a newline and then flushes the output) vs `'\n'` or `"\n"`. Check out https://stackoverflow.com/questions/8311058/n-or-n-or-stdendl-to-stdcout or http://stackoverflow.com/questions/213907/c-stdendl-vs-n – Fabio says Reinstate Monica Nov 18 '15 at 10:00
  • the simplest fix is to always end the printf() format string (that ends an output line) with '\n' as that always forces the output buffer (in the OS) to be written. A second method is when prompting the user for an input, an input operation (like scanf()) after a printf() will force the output buffer (in the OS) to be written. – user3629249 Nov 19 '15 at 19:22

4 Answers4

4

Yes it will slow it down. If it didn't, then flushing would be the default behavior.

Judging by the tone of your post, it does not sound like you're doing the kinds of things where too much flushing would be noticeable. So unless you say why you're afraid of a slowdown, flush away.

Adam
  • 16,808
  • 7
  • 52
  • 98
4

Dependant on a couple of things.

If your printf ends with a newline character (\n), the stdout buffer will flush immedeatly and display all directly. This is default behaviour of the stdout buffer. So in that case flushing again would indeed slow your program down a little, albeit only a tiny amount.

Now, if you don't end on a newline character, stdout will not flush automatically and you indeed need the fflush to display things properly. It will then also slow down the program, albeit again, only a little.

You can completly avert your problem though, by setting the buffer stdout to not wait for newlines before flushing. This would make your wrapper redundant Like this:

setbuf(stdout, NULL);

Will guarantee every time there is anything in stdout it will be flushed. This will be slightly more efficient then your direct call to fflush() every time.

In conclusion, unless you are operating on very tight performance constraints, the overhead generated is negligible.

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • actually, forcing the OS to process the output buffer every time is a drain on the system resources that is not there when the output is buffered. So, in general, this line: `setbuf(stdout, NULL);` will result in slower overall execution as the printf() cannot return until the output operation is completed. – user3629249 Nov 19 '15 at 19:25
3

Yes there will be slowdown, not necessarily very noticeable; but it will exists.

A better alternative to printf(); fflush(); would be changing the buffering policy with setvbuf(); a call to setvbuf(stdout,NULL,_IONBF,0); will assure that every write to stdout will be directly flushed (better than call explicitly fflush() each time).

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
2

IO is generally the most time consuming operation. So unnecessary flush could slow down your program. But you should thing twice about it because if output is a terminal, flush is automatic when encoutering a new-line or before a read (on same terminal...). So the only use cases I can imagine could be:

  • outputting a single char (. or * or ...) regularly to show progression of a lenghty operation. Without the flush, nothing will be seen
  • expecting the program output to be piped to another program (tee for example) while wanting progress to appear immediately on screen

TL/DR: for common usages, the slowing down will not be noticiable, but it is useless.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Another use case (my one) is where you print: "waiting for x..." and then "done\n" keeps it on the same line and there can be indefinite time between the two prints :) – code_fodder Nov 18 '15 at 14:39