printf比std :: cout快5倍以上? [英] printf more than 5 times faster than std::cout?

查看:182
本文介绍了printf比std :: cout快5倍以上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include< iostream> 
#include< cstdlib>
#include< cstdio>
#include< ctime>

int main(int argc,char * argv [])
{
std :: clock_t start;
double duration;

std :: cout<< 启动std :: cout测试。 << 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<< 结束std :: cout测试。 << std :: endl;
std :: cout<< 所用时间:<持续时间< std :: endl;

std :: system(pause);

std :: cout<< 启动std :: printf测试。 << std :: endl;
start = std :: clock();

for(int i = 0; i <1000; i ++)
{
std :: printf(Hello,World!(%i)\\\
一世);
std :: fflush(stdout);
}

duration =(std :: clock() - start)/(double)CLOCKS_PER_SEC;

std :: cout<< Ending std :: printf test。 << std :: endl;
std :: cout<< 所用时间:<持续时间< std :: endl;

系统(pause);

return 0;
}

现在,这里是前五次运行的时间:




  • std :: cout test: 1.125 s; printf测试: 0.195 s

  • std :: cout test: 1.154 printf测试: 0.230 s

  • std :: cout test: 1.142 printf测试: 0.216 s

  • std :: cout test: 1.322 printf测试: 0.221 s

  • std :: cout test: 1.108 printf test: 0.232 s



正如你所看到的,使用 printf 然后 fflush ing比使用 std :: cout 大约少5倍的时间。



虽然我希望使用 std :: cout < / code>运算符可能有点慢(几乎最小),我没有准备好这个巨大的差异。我做一个公平的测试?如果是这样,那么什么使第一个测试比第二个测试慢得多,如果他们基本上做同样的事情?

解决方案

对于真正的苹果对苹果比较,重写你的测试,以便在测试用例之间改变的只是使用的打印函数:

  int main(int argc,char * argv [])
{
const char * teststring =Test output string\
std :: clock_t start;
double duration;

std :: cout<< 启动std :: cout测试。 << std :: endl;
start = std :: clock();

for(int i = 0; i <1000; i ++)
std :: cout<测试串
/ *显示时间结果,代码为简洁修剪* /

for(int i = 0; i <1000; i ++){
std :: printf(teststring) ;
std :: fflush(stdout);
}
/ *显示时序结果,代码为了简洁* /
return 0;
}

这样,你将只测试 printf cout 函数调用。您不会因多个<< 电话等造成任何差异。如果您尝试这样做,我怀疑您会得到一个不同的结果。 p>

#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;
}

Now, here are the times for the first five runs:

  • std::cout test: 1.125 s ; printf test: 0.195 s
  • std::cout test: 1.154 s ; printf test: 0.230 s
  • std::cout test: 1.142 s ; printf test: 0.216 s
  • std::cout test: 1.322 s ; printf test: 0.221 s
  • std::cout test: 1.108 s ; printf test: 0.232 s

As you can see, using printf and then fflushing takes about 5 times less time than using std::cout.

Although I did expect using std::cout's << operator to be perhaps a little slower (almost minimal) , I wasn't prepared for this huge difference. Am I making a fair test? If so, then what makes the first test so much slower than the second one, if they essentially do the exact same thing?

解决方案

For a true apples-to-apples comparison, re-write your test so that the only thing changing between the test cases is the print function being used:

int main(int argc, char* argv[])
{
    const char* teststring = "Test output string\n";
    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;
}

With that, you will be testing nothing but the differences between the printf and cout function calls. You won't incur any differences due to multiple << calls, etc. If you try this, I suspect that you'll get a much different result.

这篇关于printf比std :: cout快5倍以上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆