cout or printf which of the two has a faster execution speed C++?

后端 未结 15 2587
旧时难觅i
旧时难觅i 2020-11-27 07:04

I have been coding in C++ for a long time. I always wondered which has a faster execution speed printf or cout?

Situation: I am designing a

相关标签:
15条回答
  • 2020-11-27 07:35

    Under the hood, they will both use the same code, so speed differences will not matter.

    If you are running on Windows only, the non-standard cprintf() might be faster as it bypasses a lot of the streams stuff.

    However it is an odd requirement. Nobody can read that fast. Why not write output to a file, then the user can browse the file at their leisure?

    0 讨论(0)
  • 2020-11-27 07:38

    Why don't you do an experiment? On average for me, printing the string helloperson;\n using printf takes, on average, 2 clock ticks, while cout using endl takes a huge amount of time - 1248996720685 clock ticks. Using cout with "\n" as the newline takes only 41981 clock ticks. The short URL for my code is below:

    cpp.sh/94qoj

    link may have expired.

    To answer your question, printf is faster.

    #include <iostream>
    #include <string>
    #include <ctime>
    #include <stdio.h>
    using namespace std;
    int main()
    {
      clock_t one;
      clock_t two;
      clock_t averagePrintf;
      clock_t averageCout;
      clock_t averagedumbHybrid;
      for (int j = 0; j < 100; j++) {
        one = clock();
        for (int d = 0; d < 20; d++) {
          printf("helloperson;");
          printf("\n");
        }
        two = clock();
        averagePrintf += two-one;
    
        one = clock();
        for (int d = 0; d < 20; d++) {
          cout << "helloperson;";
          cout << endl;
        }
        two = clock();
        averageCout += two-one;
    
        one = clock();
        for (int d = 0; d < 20; d++) {
          cout << "helloperson;";
          cout << "\n";
        }
        two = clock();
        averagedumbHybrid += two-one;
      }
      averagePrintf /= 100;
      averageCout /= 100;
      averagedumbHybrid /= 100;
      cout << "printf took " << averagePrintf << endl;
      cout << "cout took " << averageCout << endl;
      cout << "hybrid took " << averagedumbHybrid << endl;
    }
    

    Yes, I did use the word dumb. I first made it for myself, thinking that the results were crazy, so I searched it up, which ended up with me posting my code.

    Hope it helps, Ndrewffght

    0 讨论(0)
  • 2020-11-27 07:40

    Do you really need to care which has a faster execution speed? They are both used simply for printing text to the console/stdout, which typically isn't a task that demands ultra-high effiency. For that matter, I wouldn't imagine there to be a large difference in speed anyway (though one might expect printf to be marginally quicker because it lacks the minor complications of object-orientedness). Yet given that we're dealing with I/O operations here, even a minor difference would probably be swamped by the I/O overhead. Certainly, if you compared the equivalent methods for writing to files, that would be the case.

    printf is simply the standard way to output text to stdout in C.
    'cout' piping is simply the standard way to output text to stdout in C++.

    Saying all this, there is a thread on the comp.lang.cc group discussing the same issue. Consensus does however seem to be that you should choose one over the other for reasons other than performance.

    0 讨论(0)
提交回复
热议问题