Increase buffer for cout

Quoting Does setbuf () include cout?

I want to increase the buffer size to improve cout performance (this is usually redirected to disk)

Is it possible:

std::cout.rdbuf()->pubsetbuf(some_buffer, buffer_size);

      

And

ios::sync_with_stdio(false);

      

It makes sense?

EDIT: I also use multiple threads, so I was hoping to reduce the need for synchronization.

+2


source to share


1 answer


First I have to check the number of flushes that would make any larger size unnecessary.

Look especially if you have a lot of cout <endl in your code and try replacing them with cout <<'\ n' instead if you don't want endl flush effect.

As a last resort, before trying to "optimize" your search for the root cause, for example. try strace or a similar tool to see the number of system calls actually occurring. (I hope this helps your problem).



Only if all of this is already in progress, a larger buffer size can help reduce the number of system calls.

Another slowdown in cout output is that it is often ready to synchronize the output with multiple threads, even if you are using only one thread. This can again slow down I / O a lot due to overhead when a larger buffer is useless.

+4


source







All Articles