C++ does printing to terminal significantly slow down code?(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.
这篇关于C++ 打印到终端会显着减慢代码速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 打印到终端会显着减慢代码速度吗?


基础教程推荐
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01