C++ 打印到终端会显着减慢代码速度吗?

2021-12-28 00:00:00 performance printing terminal c++

我有一个代码,我目前在其中向终端打印了很多诊断消息.有人知道这会减慢我的代码速度吗?通过将输出管道传输到文件,我是否会大大提高速度,例如而不是运行:

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

我跑

./my_program > output.log

另外,通过将 cout 替换为 ofstream 并直接写入文件,我会进一步提高速度吗?

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

假设我正在写入/dev/shm,磁盘访问速度不是真正的问题.

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

推荐答案

是的,渲染到屏幕比写入文件花费的时间更长.
在 Windows 中它甚至更慢,因为程序渲染不是正在运行的程序,因此进程之间不断发送消息以获取它.
我猜它在 linux 中是一样的,因为虚拟终端与正在运行的进程位于不同的进程上.

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.

相关文章