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, e.g. 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.
It will be slower as you are having to perform a large number of prints, any extra processing is going to incur some performance penalty. Send item to a socket queue : the program will finish the writes first, and the console from the socket will print the output with a lag.
Writing to the console is faster, but if the write operation is all that's performed, then a program writing to a file will probably finish first.
When we are directly writing outputs to our terminal, each writing operation is being done “synchronously”, which means our programs waits for the “write” to complete before it continues to the next commands. Each time our programs writes something to stdout , we are met with this delay.
It's almost certainly true. Writing to the terminal is notorious for slowing things down. Run your program and redirect the output to a file and see how much faster it is.
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.
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