简体   繁体   中英

C++ does printing to terminal significantly slow down code?

I have a code where I currently print a lot of diagnostic messages to terminal. Does anybody have any idea how much this slows down my code? Would I get a big speed increase by piping the output to file, eg instead of running:

./my_program

i run

./my_program > output.log

Also, would I get a further speed increase by replacing cout with ofstream and writing to file directly?

EDIT: Let's assume I am writing to /dev/shm, disk access speed not really an issue.

Yes, rendering to screen takes longer than writing to file.
In windows its even slower as the program rendering is not the program that is running, so there are constantly messages sent between processes to get it drawn.
I guess its same in linux since virtual terminal is on a different process than the one that is running.

It certainly can be. Printing to a terminal involves rendering and other things (non-trivial) and is typically buffered a lot less. The OS and stream implementation can do a lot more buffering and caching with file I/O.

How much of a speed hit you get will depend on several factors. The Windows console, for instance, is notorously slow. ofstream might be slighlty faster than cout , depending on yor libc++ implementation (different buffer sizes, thread synchronization, ...)

This is hard to say without measuring a particular system, but I suspect that writing to a file would in fact be faster than writing to the display (files don't have to scroll etc).

Depending on your OS, system, and libraries, writing to an ofstream directly could increase performance further if it uses a different buffering scheme compared to cout but it might have no effect whatsoever. The only way to know for sure is to profile your code (as already suggested in a comment).

It really depends on how much you are printing.

If your program is printing 50 or more lines per second then I bet output starts to become significant.

Output to a file is definitely much faster than printing to a terminal, though different terminal programs will vary significantly in their speed, depending on how much rendering they are doing and what they use for the rendering api.

I very much doubt there is any significant performance difference for cout vs. ofstream for performance of terminal printing or even output to a file. There might be a very small performance gain if you wrote log lines using fwrite. Ultimately things like cout will call fwrite, so you can get a small improvement by just calling down to that lowest level yourself.

Finally - output streams like cout are faster than error streams like cerr. Cout will do more buffering than cerr, performance can be significantly faster. But it looks like you are using cout already.

Generally, yes. If you don't have to write to the terminal, you can use a file. You can also use /dev/null if there is no need to see the output.(For example, to measure real speed ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM