3

Using cout needs a little bit more time to output the statement which isn't good for me. But when using cerr the output is faster. Why?

chrk
  • 4,037
  • 2
  • 39
  • 47
P. Balech
  • 105
  • 1
  • 2
  • 7
  • 9
    In Linux `std::cout` / `stdout` is line buffered while `std::cerr` / `stderr` is not buffered, see http://stackoverflow.com/questions/3723795/is-stdout-line-buffered-unbuffered-or-indeterminate-by-default – simon Jan 07 '16 at 19:27
  • 4
    How did you determine that it is “faster”? – 5gon12eder Jan 07 '16 at 19:28
  • 1
    My guess is that output to `std::cerr` or to `std::clog` should be *slower* than to `std::cout` (because of buffering differences); did you flush the output with `< – Basile Starynkevitch Jan 07 '16 at 19:29
  • Faster might mean "appears sooner". – Bo Persson Jan 07 '16 at 19:41
  • Yes appears sooner .. – P. Balech Jan 07 '16 at 19:42
  • 1
    Then flush the stream as @BasileStarynkevitch said if you need the output to be visible immediately. Note that this comes at the cost of lower overall performance but you can probably tolerate this for an interactive program. – 5gon12eder Jan 07 '16 at 19:50

1 Answers1

3

Just trying to help : - cout -> Regular output (console output) - cerr -> Error output (console error)

cout is buffered, cerr is not, so cout should be faster in most cases. (Although if you really care about speed, the C output functions such as printf tend to be a lot faster than cout/cerr). cout and cerr are ostream objects. You can call rdbuf() on them to redirect their output independently wherever you want, from within the application. You can open a network socket, wrap it in a stream buffer and redirect there, if you want.

By default, cout is tied to the application's standard output. By default, the standard output is the screen. You can direct the OS to redirect stdout elsewhere. Or it might do it by itself - the nohup utility in Linux, for example, does. Services in Windows also have their standard streams redirected, I think.

And, cerr are tied to the application's standard error. By default the standard error is the screen. You can again redirect stderr elsewhere. Another issue here is that clog, by default, is buffered like cout, whereas cerr is unit-buffered, meaning it automatically calls flush() after every complete output operation. This is very useful, since it means that the output is not lost in the buffer if the application crashes directly afterwards.

If you run a program like this: yourprog > yourfile

What you write to cout will go to yourfile. What you write to cerr will go to your screen. That's usually a good thing. I probably don't want your error messages mixed in with your program output. (Especially if some of your error messages are just warnings or diagnostic stuff). It's also possible to redirect cout to 1 file, and cerr to another. That's a handy paradigm: I run your program, redirect output to a file, error messages to a different file. If your program returns 0 from main, then I know it's OK to process the output file. If it returns an error code, I know NOT to process the output file. The error file will tell me what went wrong.

reference : - http://www.tutorialspoint.com/cplusplus/cpp_basic_input_output.htm - http://cboard.cprogramming.com/cplusplus-programming/91613-cout-cerr-clog.html

  • 2
    `cout` is not slower than `printf` on modern systems, it's a myth; just call `std::ios::sync_with_stdio(false);` before using it. – kfx Jan 07 '16 at 19:54
  • @kfx the reason it is slower is that it waits for printf. If `std::sync_with_stdio(0);` is used then infact, it is faster than printf – Abhay Patil Aug 30 '20 at 10:48