打印到控制台vs写入文件(速度) [英] Printing to the console vs writing to a file (speed)
问题描述
在C ++中,如果重复,比如5000次,它会更快:
In C++, which would be faster if repeated, say, 5000 times:
cout << "text!" << endl;
或
my_text_file << "text!" << endl;
(写入文件而不是控制台)
(writing to a file vs. cout-ing to the console)
编辑:
我问,因为写入控制台时,看到所有正在打印的文本似乎会减慢循环。
I ask because when writing to the console, you see all the text being printed which seems like it would slow down the loop. In a file, you arn't seeing the text being printed, which seems as if it would take less time.
只是测试过:
控制台:>使用endl和\ n
Console: > 2000 ms using endl and \n
文件:40 ms >
File: 40 ms with endl and 4 ms with \n
推荐答案
写入文件会更快。这是特别真实的,因为你在 endl
的每一行之后刷新缓冲区。
Writing to a file would be much faster. This is especially true since you are flushing the buffer after every line with endl
.
可以通过重复 cout<< text!\\\
5000次,然后使用
; flush()
刷新缓冲区。
On a side note, you could speed the printing significantly by doing repeating cout << "text!\n";
5000 times, then flushing the buffer using flush()
.
这篇关于打印到控制台vs写入文件(速度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!