cout
is a buffered stream. This means that the data will be written to the buffer and will be printed when the stream is flushed, program terminated or when the buffer is completely filled.
I made a small program to test how this works, but I don't understand why it prints even before any of the above conditions are met.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
cout << "Test";
float secs = 5;
clock_t delay = secs * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay) { }
return 0;
}
When run, "Test" is output before the loop begins.
Why is my output not buffered until the program terminates?
This may happen because std::cout is writing to output buffer which is waiting to be flushed. If no flushing occurs nothing will print. So you may have to flush the buffer manually by doing the following: std::cout.
In C++, we can explicitly be flushed to force the buffer to be written. Generally, the std::endl function works the same by inserting a new-line character and flushes the stream. stdout/cout is line-buffered that is the output doesn't get sent to the OS until you write a newline or explicitly flush the buffer.
As for why it is so "time consuming", (in other words, slow,) that's because the primary purpose of std::cout (and ultimately the operating system's standard output stream) is versatility, not performance.
std::cout was not buffered. The state of ios::unitbuf on std::cout is not explicitly defined (thus implementation dependent).
There's a great discussion on this over here.
From one of the answers:
Every C++ stream uses an associated stream buffer object to perform buffering.
When
std::cout
is constructed, it uses the stream buffer associated with the objectstdout
, declared in<cstdio>
. By default, operations onstd::cout
can be freely mixed with<cstdio>
output functions likestd::printf()
.In practical terms, synchronization usually means that a standard iostream object and a standard stdio object share a buffer. - IS
If
std::ios_base::sync_with_stdio(false)
is called (before any input or output operations on the standard streams), the standard C++ streams operate independently of the standard C streams (ie. they switch to their own separate stream buffers).
For more, see the sync_with_stdio
function reference page on cppreference.
From that page, the function...
Sets whether the standard C++ streams are synchronized to the standard C streams after each input/output operation.
...In practice, this means that the synchronized C++ streams are unbuffered, and each I/O operation on a C++ stream is immediately applied to the corresponding C stream's buffer. This makes it possible to freely mix C++ and C I/O.
However, beware of calling this function after there have already been reads or writes:
If this function is called after I/O has occurred on the standard stream, the behavior is implementation-defined: implementations range from no effect to destroying the read buffer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With