printf 比 std::cout 快 5 倍以上
printf more than 5 times faster than std::cout?
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
int main(int argc, char* argv[])
{
std::clock_t start;
double duration;
std::cout << "Starting std::cout test." << std::endl;
start = std::clock();
for (int i = 0; i < 1000; i++)
{
std::cout << "Hello, World! (" << i << ")" << std::endl;
}
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Ending std::cout test." << std::endl;
std::cout << "Time taken: " << duration << std::endl;
std::system("pause");
std::cout << "Starting std::printf test." << std::endl;
start = std::clock();
for (int i = 0; i < 1000; i++)
{
std::printf("Hello, World! (%i)n", i);
std::fflush(stdout);
}
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Ending std::printf test." << std::endl;
std::cout << "Time taken: " << duration << std::endl;
system("pause");
return 0;
}
现在,以下是前五次运行的时间:
- 标准::COUT测试: 1.125 s ; 打印F测试:0.195秒
- 标准::COUT测试: 1.154 s ; 打印F测试:0.230 s
- 标准::COUT测试: 1.142 s ; 打印F测试:0.216 s
- 标准::COUT测试: 1.322 s ; 打印F测试:0.221秒
- 标准::COUT测试: 1.108 s ; 打印F测试:0.232秒
如您所见,使用printf
然后fflush
ing所需的时间大约是使用std::cout
的5倍。
虽然我确实期望使用std::cout
的<<
运算符可能会慢一点(几乎最小(,但我并没有为这种巨大的差异做好准备。我是否在做公平的测试?如果是这样,那么如果它们本质上做完全相同的事情,那么是什么让第一个测试比第二个测试慢得多?
试试这个:
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <iostream>
int main(int argc, char* argv[])
{
#if defined(NOSYNC)
std::cout.sync_with_stdio(false);
#endif
std::cout << "Starting std::cout test." << std::endl;
std::clock_t start = std::clock();
for (int i = 0; i < 1000; i++)
{
std::cout << "Hello, World! (" << i << ")" << std::endl;
}
clock_t mid = std::clock();
for (int i = 0; i < 1000; i++)
{
std::printf("Hello, World! (%i)n", i);
std::fflush(stdout);
}
std::clock_t end = std::clock();
std::cout << "Time taken: P1 " << ((mid-start)*1.0/CLOCKS_PER_SEC) << std::endl;
std::cout << "Time taken: P2 " << ((end-mid)*1.0/CLOCKS_PER_SEC) << std::endl;
return 0;
}
然后我得到:
> g++ -O3 t13.cpp
> ./a.out
# lots of lines deleted
Time taken: P1 0.002517
Time taken: P2 0.001872
> g++ -O3 t13.cpp -DNOSYNC
> ./a.out
# lots of lines deleted
Time taken: P1 0.002398
Time taken: P2 0.001878
所以P2时间不会改变。
但是您可以使用 std::cout.sync_with_stdio(false);
获得 P1 倍的改进(即 std::cout(。因为代码不再尝试保持两个流(std::cout stdout(同步。如果你写的是纯C++并且只使用 std::cout 这不是问题。
对于真正的同类比较,请重写测试,以便测试用例之间唯一更改的是正在使用的打印函数:
int main(int argc, char* argv[])
{
const char* teststring = "Test output stringn";
std::clock_t start;
double duration;
std::cout << "Starting std::cout test." << std::endl;
start = std::clock();
for (int i = 0; i < 1000; i++)
std::cout << teststring;
/* Display timing results, code trimmed for brevity */
for (int i = 0; i < 1000; i++) {
std::printf(teststring);
std::fflush(stdout);
}
/* Display timing results, code trimmed for brevity */
return 0;
}
这样,您将只测试printf
和cout
函数调用之间的差异。 您不会因多次<<
调用等而产生任何差异。 如果你尝试这个,我怀疑你会得到一个完全不同的结果。
使用
cout << "n";
以防止缓冲。 快得多
大约10年前,Scott Meyers测试了iostream和scanf/printf的效率。取决于编译器和环境,有时scanf/printf比iostream快20%,有时甚至快200%。但是iostream从来没有比scanf/printf快。根据另外两个给出的例子,scanf/printf 仍然比 iostream 快。然而,迈耶斯说:"在一个真正有用的程序上,不会有任何区别。根据Google的编程风格([http://google-styleguide.googlecode.com/svn/trunk/cppguide.html](,除了日志记录之外,不应使用流。iostream的替代品是自己封装scanf/printf。
我只有 1 台计算机的编程范围,所以测试不多。无论如何,std::cout 在我的构建中使用确切的详细代码要快得多。我正在使用 Cpp14。
我只是认为某些人有C++的选择。是的,C 是很棒的语言,但从逻辑上讲,我不明白 printf 如何比 std cout 更快。Printf 必须在运行时从 void 指针进行类型转换。Cout 在编译时执行此操作。
我在笔记本电脑上尝试了这个测试,运行Windows 10,WSL Ubuntu,CLion 2018,GCC。无优化:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
int main(int argc, char *argv[]) {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::clock_t start;
double duration1, duration2;
std::cout << "Starting std::cout test.n";
start = std::clock();
for (int i = 0; i < 100000; i++) {
std::cout << "Hello, World! (" << i << ")" << std::endl;
}
duration1 = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Starting std::printf test.n";
start = std::clock();
for (int i = 0; i < 100000; i++) {
std::printf("Hello, World! (%i)n", i);
std::fflush(stdout);
}
duration2 = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Time taken: cout " << duration1 << std::endl;
std::cout << "Time taken Printf: " << duration2 << std::endl;
return 0;
}
结果:
Test1: Cout: 2.25, Printf: 2.45312 (Cout run first)
Test2: Cout: 2.42188, Printf: 2.07812 (Printf Run first)
Test3: Cout: 2.26562, Printf: 2.25 (Cout run first)
Test4: Cout 2.46875, Printf: 2.57812 (Printf run first)
DR:使用std::ios_base::sync_with_stdio(false)
和std::cin.tie(nullptr)
将两种功能带到几乎相同的位置。
- std::cout.imbue()多重调用
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- std::cout输出int时出现编译错误
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- 为什么 std::cout 打印浮点数、双精度和长双精度到相同的小数精度?
- 为什么我需要做'using namespace std'而不是"使用 std::cout"?
- main() 中的 std::cout 在调试期间不会在调试控制台中打印任何内容
- 错误:在尝试检测 std::cout 是否<< t 时,功能强制转换为数组类型;有效
- std::cout 来自多个线程
- 在 c++ 中以十六进制格式打印无符号字符(BYTE).使用 std::cout
- Cuda 基本程序 (将值写入矩阵和 std:cout 不起作用) ;主功能不启动
- 使用 std::addressof(std::cout) 而不是 &std::cout 是否有任何风险?
- 是否可以为 std::cout 创建别名?
- std::cout 在打印变量与函数表达式时的行为不同
- 带有 std::cout 的多线程控制台文本动画
- 为什么在语句"std::cout << std::endl;"中使用时需要命名空间限定,给定依赖于参数的查找?
- "std::cout << std::endl;"如何编译?
- std::cout/std::cerr seg faults