printf 比 std::cout 快 5 倍以上

printf more than 5 times faster than std::cout?

本文关键字:cout std printf      更新时间:2023-10-16
#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;
}

这样,您将只测试printfcout函数调用之间的差异。 您不会因多次<<调用等而产生任何差异。 如果你尝试这个,我怀疑你会得到一个完全不同的结果。

使用

cout << "n";

以防止缓冲。 快得多

大约10年前,Scott Meyers测试了iostreamscanf/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)将两种功能带到几乎相同的位置。