printf more than 5 times faster than std::cout?printf with std::string?Big difference (x9) in the execution time between almost identical code in C and C++C++ iostream vs. C stdio performance/overheadwhich is faster, and which is more flexible: printf or cout?Why is snprintf consistently 2x faster than ostringstream for printing a single number?When is assembly faster than C?mixing cout and printf for faster output'printf' vs. 'cout' in C++Why are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?Is < faster than <=?Measure CPU time and wall clock time of a program in C++Why is [] faster than list()?C++ code for testing the Collatz conjecture faster than hand-written assembly - why?Why is 2 * (i * i) faster than 2 * i * i in Java?

Local variables in DynamicModule affected by outside evaluation

Is "vegetable base" a common term in English?

What is the limit to a Glyph of Warding's trigger?

What is to the west of Westeros?

Is keeping the forking link on a true fork necessary (Github/GPL)?

Cisco 3750X Power Cable

Have any humans orbited the Earth in anything other than a prograde orbit?

Are there historical examples of audiences drawn to a work that was "so bad it's good"?

Where is Jon going?

What is the purpose of the yellow wired panels on the IBM 360 Model 20?

Is superuser the same as root?

Comparison of bool data types in C++

Why is std::ssize() introduced in C++20?

To exponential digit growth and beyond!

Are cells guaranteed to get at least one mitochondrion when they divide?

Piping the output of comand columns

Are there guidelines for finding good names for LaTeX 2e packages and control sequences defined in these packages?

Alexandrov's generalization of Cauchy's rigidity theorem

Why do testers need root cause analysis?

Fill area of x^2+y^2>1 and x^2+y^2>4 using patterns and tikzpicture

Why is this integration method not valid?

Count all vowels in string

Navigating a quick return to previous employer

Did significant numbers of Japanese officers escape prosecution during the Tokyo Trials?



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


printf with std::string?Big difference (x9) in the execution time between almost identical code in C and C++C++ iostream vs. C stdio performance/overheadwhich is faster, and which is more flexible: printf or cout?Why is snprintf consistently 2x faster than ostringstream for printing a single number?When is assembly faster than C?mixing cout and printf for faster output'printf' vs. 'cout' in C++Why are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?Is < faster than <=?Measure CPU time and wall clock time of a program in C++Why is [] faster than list()?C++ code for testing the Collatz conjecture faster than hand-written assembly - why?Why is 2 * (i * i) faster than 2 * i * i in Java?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








13















#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?










share|improve this question

















  • 2





    He did flush in the printf version too though, so that can't be it?

    – Cubic
    Aug 20 '12 at 20:12






  • 2





    The possible duplicate question does not answer my main question, **what makes** the first test so much slower than the second one... i.e why exactly is the printf faster?

    – ApprenticeHacker
    Aug 20 '12 at 20:19







  • 1





    There may not be a single correct answer other than the quality of the library implementation you're using. There have been similar questions such as this linked one. Read it and the related questions it links to.

    – Blastfurnace
    Aug 20 '12 at 20:31






  • 3





    Note that std::cout << "Hello, World! (" << i << ")" << std::endl; is 4 function calls, while printf is only one. Try doing printf 5 times, like with cout and see what happens. Then you'll realize how much of the performance fail is due to function calls, and how much due the library itself.

    – Shahbaz
    Aug 20 '12 at 20:35







  • 2





    Your current timings are meaningless as the std::cout is synced with stdout and thus does a lot of "extra" work to maintain synchronization. If you decouple them you will see a speedup (it is still slower) std::cout.sync_with_stdio(false);

    – Martin York
    Aug 21 '12 at 15:34

















13















#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?










share|improve this question

















  • 2





    He did flush in the printf version too though, so that can't be it?

    – Cubic
    Aug 20 '12 at 20:12






  • 2





    The possible duplicate question does not answer my main question, **what makes** the first test so much slower than the second one... i.e why exactly is the printf faster?

    – ApprenticeHacker
    Aug 20 '12 at 20:19







  • 1





    There may not be a single correct answer other than the quality of the library implementation you're using. There have been similar questions such as this linked one. Read it and the related questions it links to.

    – Blastfurnace
    Aug 20 '12 at 20:31






  • 3





    Note that std::cout << "Hello, World! (" << i << ")" << std::endl; is 4 function calls, while printf is only one. Try doing printf 5 times, like with cout and see what happens. Then you'll realize how much of the performance fail is due to function calls, and how much due the library itself.

    – Shahbaz
    Aug 20 '12 at 20:35







  • 2





    Your current timings are meaningless as the std::cout is synced with stdout and thus does a lot of "extra" work to maintain synchronization. If you decouple them you will see a speedup (it is still slower) std::cout.sync_with_stdio(false);

    – Martin York
    Aug 21 '12 at 15:34













13












13








13


4






#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?










share|improve this question














#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?







c++ performance printf cout






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 20 '12 at 20:06









ApprenticeHackerApprenticeHacker

11k2084138




11k2084138







  • 2





    He did flush in the printf version too though, so that can't be it?

    – Cubic
    Aug 20 '12 at 20:12






  • 2





    The possible duplicate question does not answer my main question, **what makes** the first test so much slower than the second one... i.e why exactly is the printf faster?

    – ApprenticeHacker
    Aug 20 '12 at 20:19







  • 1





    There may not be a single correct answer other than the quality of the library implementation you're using. There have been similar questions such as this linked one. Read it and the related questions it links to.

    – Blastfurnace
    Aug 20 '12 at 20:31






  • 3





    Note that std::cout << "Hello, World! (" << i << ")" << std::endl; is 4 function calls, while printf is only one. Try doing printf 5 times, like with cout and see what happens. Then you'll realize how much of the performance fail is due to function calls, and how much due the library itself.

    – Shahbaz
    Aug 20 '12 at 20:35







  • 2





    Your current timings are meaningless as the std::cout is synced with stdout and thus does a lot of "extra" work to maintain synchronization. If you decouple them you will see a speedup (it is still slower) std::cout.sync_with_stdio(false);

    – Martin York
    Aug 21 '12 at 15:34












  • 2





    He did flush in the printf version too though, so that can't be it?

    – Cubic
    Aug 20 '12 at 20:12






  • 2





    The possible duplicate question does not answer my main question, **what makes** the first test so much slower than the second one... i.e why exactly is the printf faster?

    – ApprenticeHacker
    Aug 20 '12 at 20:19







  • 1





    There may not be a single correct answer other than the quality of the library implementation you're using. There have been similar questions such as this linked one. Read it and the related questions it links to.

    – Blastfurnace
    Aug 20 '12 at 20:31






  • 3





    Note that std::cout << "Hello, World! (" << i << ")" << std::endl; is 4 function calls, while printf is only one. Try doing printf 5 times, like with cout and see what happens. Then you'll realize how much of the performance fail is due to function calls, and how much due the library itself.

    – Shahbaz
    Aug 20 '12 at 20:35







  • 2





    Your current timings are meaningless as the std::cout is synced with stdout and thus does a lot of "extra" work to maintain synchronization. If you decouple them you will see a speedup (it is still slower) std::cout.sync_with_stdio(false);

    – Martin York
    Aug 21 '12 at 15:34







2




2





He did flush in the printf version too though, so that can't be it?

– Cubic
Aug 20 '12 at 20:12





He did flush in the printf version too though, so that can't be it?

– Cubic
Aug 20 '12 at 20:12




2




2





The possible duplicate question does not answer my main question, **what makes** the first test so much slower than the second one... i.e why exactly is the printf faster?

– ApprenticeHacker
Aug 20 '12 at 20:19






The possible duplicate question does not answer my main question, **what makes** the first test so much slower than the second one... i.e why exactly is the printf faster?

– ApprenticeHacker
Aug 20 '12 at 20:19





1




1





There may not be a single correct answer other than the quality of the library implementation you're using. There have been similar questions such as this linked one. Read it and the related questions it links to.

– Blastfurnace
Aug 20 '12 at 20:31





There may not be a single correct answer other than the quality of the library implementation you're using. There have been similar questions such as this linked one. Read it and the related questions it links to.

– Blastfurnace
Aug 20 '12 at 20:31




3




3





Note that std::cout << "Hello, World! (" << i << ")" << std::endl; is 4 function calls, while printf is only one. Try doing printf 5 times, like with cout and see what happens. Then you'll realize how much of the performance fail is due to function calls, and how much due the library itself.

– Shahbaz
Aug 20 '12 at 20:35






Note that std::cout << "Hello, World! (" << i << ")" << std::endl; is 4 function calls, while printf is only one. Try doing printf 5 times, like with cout and see what happens. Then you'll realize how much of the performance fail is due to function calls, and how much due the library itself.

– Shahbaz
Aug 20 '12 at 20:35





2




2





Your current timings are meaningless as the std::cout is synced with stdout and thus does a lot of "extra" work to maintain synchronization. If you decouple them you will see a speedup (it is still slower) std::cout.sync_with_stdio(false);

– Martin York
Aug 21 '12 at 15:34





Your current timings are meaningless as the std::cout is synced with stdout and thus does a lot of "extra" work to maintain synchronization. If you decouple them you will see a speedup (it is still slower) std::cout.sync_with_stdio(false);

– Martin York
Aug 21 '12 at 15:34












6 Answers
6






active

oldest

votes


















9














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



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.






share|improve this answer























  • Hmm, now it's only a difference of about 0.06 s. Guess that answers my question.

    – ApprenticeHacker
    Aug 20 '12 at 20:50






  • 4





    @bta, it seems to be an unfair test. For printf section you are flushing stdout after each iteration, but the std::cout code is not. Should probably add << std::flush(); to the std::cout section.

    – paxos1977
    Aug 20 '12 at 21:06






  • 1





    Add the following: std::cout.sync_with_stdio(false);

    – Martin York
    Aug 20 '12 at 21:16






  • 3





    This is not a useful comparison if the performance difference between cout and printf is due to the formatting of the number i to a string.

    – joydeepb
    Oct 31 '13 at 18:46






  • 1





    If the difference is due to requiring many more << calls than printf calls, it's totally appropriate to compare them that way. It's not realistic to simplify the test to the extent that you have.

    – Mark Ransom
    Sep 2 '16 at 15:16


















12














Try this:



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



Then I get:



> 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


So the P2 times do not change.

But you get an improvement of the P1 times (ie std::cout) using std::cout.sync_with_stdio(false);. Becuase the code no longer tries to keep the two stream (std::cout stdout) synchronized. Which if you are writing pure C++ and only using std::cout not a problem.






share|improve this answer






























    3














    use



    cout << "n";


    to prevent buffering. much faster






    share|improve this answer






























      1














      About 10 years ago, Scott Meyers tested the efficiency of iostream and scanf/printf. Depends on the compiler and environment, sometimes scanf/printf is 20% faster than iostream, sometimes even 200% faster. But iostream was never faster than scanf/printf. According to other two given examples, scanf/printf is still faster than iostream.
      However, Meyers said that "On a really useful program, there would be no differences."
      According to Google''s programming style([http://google-styleguide.googlecode.com/svn/trunk/cppguide.html]), streams should not be used except for logging.
      A replacement for iostream is to encapsule scanf/printf yourself.






      share|improve this answer






























        0














        I only have a programming range of 1 computer, so not much testing. Anyhow, std::cout is way more faster on my build using the exact verbose code. I am using Cpp14.



        I just think certain people have a pick for C++. Yes C is great language but logically I don't see how printf can be faster than std cout. Printf has to do type conversions from void pointer on runtime. Cout does that at compile time.






        share|improve this answer























        • If iostreams are in fact faster on your platform, it would be useful to know what that platform is. The reason that iostreams tend to be a lot slower than printf is that iostreams use a huge number of function calls, including a lot of virtual function calls (an iostream doesn't do anything by itself, it forwards everything to a streambuf virtual function). That's very useful if you have a need to redirect the streams, but wasteful if you aren't using it.

          – Ben Voigt
          May 22 '18 at 2:27











        • Okay I don't really know what platform you refer to, but I am using Win 10, Intel Celeron CPU G3900, 4 gigs Ram, CPP 14. You should remember CPP evolves every three years. A lot of function calls are compiled away.

          – Javene CPP McGowan
          May 26 '18 at 3:58











        • Which compiler and which C++ standard library are very important details of the platform... the CPU (which you mentioned, and also contains cache information) and the RAM (which you mentioned only size but not clock rate, DDR generation, number of channels, or latency) are less important in this case but can sometimes have a big effect on performance and optimization.

          – Ben Voigt
          May 30 '18 at 3:19


















        0














        I tried this test on my laptop, running Windows 10, WSL Ubuntu, CLion 2018, GCC. No optimization:



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



        Results:



        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)


        TL;DR: Using std::ios_base::sync_with_stdio(false) and std::cin.tie(nullptr) brings both functions to almost the same stand.






        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12044357%2fprintf-more-than-5-times-faster-than-stdcout%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          9














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



          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.






          share|improve this answer























          • Hmm, now it's only a difference of about 0.06 s. Guess that answers my question.

            – ApprenticeHacker
            Aug 20 '12 at 20:50






          • 4





            @bta, it seems to be an unfair test. For printf section you are flushing stdout after each iteration, but the std::cout code is not. Should probably add << std::flush(); to the std::cout section.

            – paxos1977
            Aug 20 '12 at 21:06






          • 1





            Add the following: std::cout.sync_with_stdio(false);

            – Martin York
            Aug 20 '12 at 21:16






          • 3





            This is not a useful comparison if the performance difference between cout and printf is due to the formatting of the number i to a string.

            – joydeepb
            Oct 31 '13 at 18:46






          • 1





            If the difference is due to requiring many more << calls than printf calls, it's totally appropriate to compare them that way. It's not realistic to simplify the test to the extent that you have.

            – Mark Ransom
            Sep 2 '16 at 15:16















          9














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



          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.






          share|improve this answer























          • Hmm, now it's only a difference of about 0.06 s. Guess that answers my question.

            – ApprenticeHacker
            Aug 20 '12 at 20:50






          • 4





            @bta, it seems to be an unfair test. For printf section you are flushing stdout after each iteration, but the std::cout code is not. Should probably add << std::flush(); to the std::cout section.

            – paxos1977
            Aug 20 '12 at 21:06






          • 1





            Add the following: std::cout.sync_with_stdio(false);

            – Martin York
            Aug 20 '12 at 21:16






          • 3





            This is not a useful comparison if the performance difference between cout and printf is due to the formatting of the number i to a string.

            – joydeepb
            Oct 31 '13 at 18:46






          • 1





            If the difference is due to requiring many more << calls than printf calls, it's totally appropriate to compare them that way. It's not realistic to simplify the test to the extent that you have.

            – Mark Ransom
            Sep 2 '16 at 15:16













          9












          9








          9







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



          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.






          share|improve this answer













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



          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 20 '12 at 20:45









          btabta

          35.7k45786




          35.7k45786












          • Hmm, now it's only a difference of about 0.06 s. Guess that answers my question.

            – ApprenticeHacker
            Aug 20 '12 at 20:50






          • 4





            @bta, it seems to be an unfair test. For printf section you are flushing stdout after each iteration, but the std::cout code is not. Should probably add << std::flush(); to the std::cout section.

            – paxos1977
            Aug 20 '12 at 21:06






          • 1





            Add the following: std::cout.sync_with_stdio(false);

            – Martin York
            Aug 20 '12 at 21:16






          • 3





            This is not a useful comparison if the performance difference between cout and printf is due to the formatting of the number i to a string.

            – joydeepb
            Oct 31 '13 at 18:46






          • 1





            If the difference is due to requiring many more << calls than printf calls, it's totally appropriate to compare them that way. It's not realistic to simplify the test to the extent that you have.

            – Mark Ransom
            Sep 2 '16 at 15:16

















          • Hmm, now it's only a difference of about 0.06 s. Guess that answers my question.

            – ApprenticeHacker
            Aug 20 '12 at 20:50






          • 4





            @bta, it seems to be an unfair test. For printf section you are flushing stdout after each iteration, but the std::cout code is not. Should probably add << std::flush(); to the std::cout section.

            – paxos1977
            Aug 20 '12 at 21:06






          • 1





            Add the following: std::cout.sync_with_stdio(false);

            – Martin York
            Aug 20 '12 at 21:16






          • 3





            This is not a useful comparison if the performance difference between cout and printf is due to the formatting of the number i to a string.

            – joydeepb
            Oct 31 '13 at 18:46






          • 1





            If the difference is due to requiring many more << calls than printf calls, it's totally appropriate to compare them that way. It's not realistic to simplify the test to the extent that you have.

            – Mark Ransom
            Sep 2 '16 at 15:16
















          Hmm, now it's only a difference of about 0.06 s. Guess that answers my question.

          – ApprenticeHacker
          Aug 20 '12 at 20:50





          Hmm, now it's only a difference of about 0.06 s. Guess that answers my question.

          – ApprenticeHacker
          Aug 20 '12 at 20:50




          4




          4





          @bta, it seems to be an unfair test. For printf section you are flushing stdout after each iteration, but the std::cout code is not. Should probably add << std::flush(); to the std::cout section.

          – paxos1977
          Aug 20 '12 at 21:06





          @bta, it seems to be an unfair test. For printf section you are flushing stdout after each iteration, but the std::cout code is not. Should probably add << std::flush(); to the std::cout section.

          – paxos1977
          Aug 20 '12 at 21:06




          1




          1





          Add the following: std::cout.sync_with_stdio(false);

          – Martin York
          Aug 20 '12 at 21:16





          Add the following: std::cout.sync_with_stdio(false);

          – Martin York
          Aug 20 '12 at 21:16




          3




          3





          This is not a useful comparison if the performance difference between cout and printf is due to the formatting of the number i to a string.

          – joydeepb
          Oct 31 '13 at 18:46





          This is not a useful comparison if the performance difference between cout and printf is due to the formatting of the number i to a string.

          – joydeepb
          Oct 31 '13 at 18:46




          1




          1





          If the difference is due to requiring many more << calls than printf calls, it's totally appropriate to compare them that way. It's not realistic to simplify the test to the extent that you have.

          – Mark Ransom
          Sep 2 '16 at 15:16





          If the difference is due to requiring many more << calls than printf calls, it's totally appropriate to compare them that way. It's not realistic to simplify the test to the extent that you have.

          – Mark Ransom
          Sep 2 '16 at 15:16













          12














          Try this:



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



          Then I get:



          > 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


          So the P2 times do not change.

          But you get an improvement of the P1 times (ie std::cout) using std::cout.sync_with_stdio(false);. Becuase the code no longer tries to keep the two stream (std::cout stdout) synchronized. Which if you are writing pure C++ and only using std::cout not a problem.






          share|improve this answer



























            12














            Try this:



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



            Then I get:



            > 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


            So the P2 times do not change.

            But you get an improvement of the P1 times (ie std::cout) using std::cout.sync_with_stdio(false);. Becuase the code no longer tries to keep the two stream (std::cout stdout) synchronized. Which if you are writing pure C++ and only using std::cout not a problem.






            share|improve this answer

























              12












              12








              12







              Try this:



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



              Then I get:



              > 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


              So the P2 times do not change.

              But you get an improvement of the P1 times (ie std::cout) using std::cout.sync_with_stdio(false);. Becuase the code no longer tries to keep the two stream (std::cout stdout) synchronized. Which if you are writing pure C++ and only using std::cout not a problem.






              share|improve this answer













              Try this:



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



              Then I get:



              > 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


              So the P2 times do not change.

              But you get an improvement of the P1 times (ie std::cout) using std::cout.sync_with_stdio(false);. Becuase the code no longer tries to keep the two stream (std::cout stdout) synchronized. Which if you are writing pure C++ and only using std::cout not a problem.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 20 '12 at 21:33









              Martin YorkMartin York

              202k67271490




              202k67271490





















                  3














                  use



                  cout << "n";


                  to prevent buffering. much faster






                  share|improve this answer



























                    3














                    use



                    cout << "n";


                    to prevent buffering. much faster






                    share|improve this answer

























                      3












                      3








                      3







                      use



                      cout << "n";


                      to prevent buffering. much faster






                      share|improve this answer













                      use



                      cout << "n";


                      to prevent buffering. much faster







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Apr 28 '17 at 14:37









                      JasonJason

                      736810




                      736810





















                          1














                          About 10 years ago, Scott Meyers tested the efficiency of iostream and scanf/printf. Depends on the compiler and environment, sometimes scanf/printf is 20% faster than iostream, sometimes even 200% faster. But iostream was never faster than scanf/printf. According to other two given examples, scanf/printf is still faster than iostream.
                          However, Meyers said that "On a really useful program, there would be no differences."
                          According to Google''s programming style([http://google-styleguide.googlecode.com/svn/trunk/cppguide.html]), streams should not be used except for logging.
                          A replacement for iostream is to encapsule scanf/printf yourself.






                          share|improve this answer



























                            1














                            About 10 years ago, Scott Meyers tested the efficiency of iostream and scanf/printf. Depends on the compiler and environment, sometimes scanf/printf is 20% faster than iostream, sometimes even 200% faster. But iostream was never faster than scanf/printf. According to other two given examples, scanf/printf is still faster than iostream.
                            However, Meyers said that "On a really useful program, there would be no differences."
                            According to Google''s programming style([http://google-styleguide.googlecode.com/svn/trunk/cppguide.html]), streams should not be used except for logging.
                            A replacement for iostream is to encapsule scanf/printf yourself.






                            share|improve this answer

























                              1












                              1








                              1







                              About 10 years ago, Scott Meyers tested the efficiency of iostream and scanf/printf. Depends on the compiler and environment, sometimes scanf/printf is 20% faster than iostream, sometimes even 200% faster. But iostream was never faster than scanf/printf. According to other two given examples, scanf/printf is still faster than iostream.
                              However, Meyers said that "On a really useful program, there would be no differences."
                              According to Google''s programming style([http://google-styleguide.googlecode.com/svn/trunk/cppguide.html]), streams should not be used except for logging.
                              A replacement for iostream is to encapsule scanf/printf yourself.






                              share|improve this answer













                              About 10 years ago, Scott Meyers tested the efficiency of iostream and scanf/printf. Depends on the compiler and environment, sometimes scanf/printf is 20% faster than iostream, sometimes even 200% faster. But iostream was never faster than scanf/printf. According to other two given examples, scanf/printf is still faster than iostream.
                              However, Meyers said that "On a really useful program, there would be no differences."
                              According to Google''s programming style([http://google-styleguide.googlecode.com/svn/trunk/cppguide.html]), streams should not be used except for logging.
                              A replacement for iostream is to encapsule scanf/printf yourself.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Apr 28 '17 at 14:59









                              Tianze.ChenTianze.Chen

                              291




                              291





















                                  0














                                  I only have a programming range of 1 computer, so not much testing. Anyhow, std::cout is way more faster on my build using the exact verbose code. I am using Cpp14.



                                  I just think certain people have a pick for C++. Yes C is great language but logically I don't see how printf can be faster than std cout. Printf has to do type conversions from void pointer on runtime. Cout does that at compile time.






                                  share|improve this answer























                                  • If iostreams are in fact faster on your platform, it would be useful to know what that platform is. The reason that iostreams tend to be a lot slower than printf is that iostreams use a huge number of function calls, including a lot of virtual function calls (an iostream doesn't do anything by itself, it forwards everything to a streambuf virtual function). That's very useful if you have a need to redirect the streams, but wasteful if you aren't using it.

                                    – Ben Voigt
                                    May 22 '18 at 2:27











                                  • Okay I don't really know what platform you refer to, but I am using Win 10, Intel Celeron CPU G3900, 4 gigs Ram, CPP 14. You should remember CPP evolves every three years. A lot of function calls are compiled away.

                                    – Javene CPP McGowan
                                    May 26 '18 at 3:58











                                  • Which compiler and which C++ standard library are very important details of the platform... the CPU (which you mentioned, and also contains cache information) and the RAM (which you mentioned only size but not clock rate, DDR generation, number of channels, or latency) are less important in this case but can sometimes have a big effect on performance and optimization.

                                    – Ben Voigt
                                    May 30 '18 at 3:19















                                  0














                                  I only have a programming range of 1 computer, so not much testing. Anyhow, std::cout is way more faster on my build using the exact verbose code. I am using Cpp14.



                                  I just think certain people have a pick for C++. Yes C is great language but logically I don't see how printf can be faster than std cout. Printf has to do type conversions from void pointer on runtime. Cout does that at compile time.






                                  share|improve this answer























                                  • If iostreams are in fact faster on your platform, it would be useful to know what that platform is. The reason that iostreams tend to be a lot slower than printf is that iostreams use a huge number of function calls, including a lot of virtual function calls (an iostream doesn't do anything by itself, it forwards everything to a streambuf virtual function). That's very useful if you have a need to redirect the streams, but wasteful if you aren't using it.

                                    – Ben Voigt
                                    May 22 '18 at 2:27











                                  • Okay I don't really know what platform you refer to, but I am using Win 10, Intel Celeron CPU G3900, 4 gigs Ram, CPP 14. You should remember CPP evolves every three years. A lot of function calls are compiled away.

                                    – Javene CPP McGowan
                                    May 26 '18 at 3:58











                                  • Which compiler and which C++ standard library are very important details of the platform... the CPU (which you mentioned, and also contains cache information) and the RAM (which you mentioned only size but not clock rate, DDR generation, number of channels, or latency) are less important in this case but can sometimes have a big effect on performance and optimization.

                                    – Ben Voigt
                                    May 30 '18 at 3:19













                                  0












                                  0








                                  0







                                  I only have a programming range of 1 computer, so not much testing. Anyhow, std::cout is way more faster on my build using the exact verbose code. I am using Cpp14.



                                  I just think certain people have a pick for C++. Yes C is great language but logically I don't see how printf can be faster than std cout. Printf has to do type conversions from void pointer on runtime. Cout does that at compile time.






                                  share|improve this answer













                                  I only have a programming range of 1 computer, so not much testing. Anyhow, std::cout is way more faster on my build using the exact verbose code. I am using Cpp14.



                                  I just think certain people have a pick for C++. Yes C is great language but logically I don't see how printf can be faster than std cout. Printf has to do type conversions from void pointer on runtime. Cout does that at compile time.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered May 22 '18 at 2:22









                                  Javene CPP McGowanJavene CPP McGowan

                                  515




                                  515












                                  • If iostreams are in fact faster on your platform, it would be useful to know what that platform is. The reason that iostreams tend to be a lot slower than printf is that iostreams use a huge number of function calls, including a lot of virtual function calls (an iostream doesn't do anything by itself, it forwards everything to a streambuf virtual function). That's very useful if you have a need to redirect the streams, but wasteful if you aren't using it.

                                    – Ben Voigt
                                    May 22 '18 at 2:27











                                  • Okay I don't really know what platform you refer to, but I am using Win 10, Intel Celeron CPU G3900, 4 gigs Ram, CPP 14. You should remember CPP evolves every three years. A lot of function calls are compiled away.

                                    – Javene CPP McGowan
                                    May 26 '18 at 3:58











                                  • Which compiler and which C++ standard library are very important details of the platform... the CPU (which you mentioned, and also contains cache information) and the RAM (which you mentioned only size but not clock rate, DDR generation, number of channels, or latency) are less important in this case but can sometimes have a big effect on performance and optimization.

                                    – Ben Voigt
                                    May 30 '18 at 3:19

















                                  • If iostreams are in fact faster on your platform, it would be useful to know what that platform is. The reason that iostreams tend to be a lot slower than printf is that iostreams use a huge number of function calls, including a lot of virtual function calls (an iostream doesn't do anything by itself, it forwards everything to a streambuf virtual function). That's very useful if you have a need to redirect the streams, but wasteful if you aren't using it.

                                    – Ben Voigt
                                    May 22 '18 at 2:27











                                  • Okay I don't really know what platform you refer to, but I am using Win 10, Intel Celeron CPU G3900, 4 gigs Ram, CPP 14. You should remember CPP evolves every three years. A lot of function calls are compiled away.

                                    – Javene CPP McGowan
                                    May 26 '18 at 3:58











                                  • Which compiler and which C++ standard library are very important details of the platform... the CPU (which you mentioned, and also contains cache information) and the RAM (which you mentioned only size but not clock rate, DDR generation, number of channels, or latency) are less important in this case but can sometimes have a big effect on performance and optimization.

                                    – Ben Voigt
                                    May 30 '18 at 3:19
















                                  If iostreams are in fact faster on your platform, it would be useful to know what that platform is. The reason that iostreams tend to be a lot slower than printf is that iostreams use a huge number of function calls, including a lot of virtual function calls (an iostream doesn't do anything by itself, it forwards everything to a streambuf virtual function). That's very useful if you have a need to redirect the streams, but wasteful if you aren't using it.

                                  – Ben Voigt
                                  May 22 '18 at 2:27





                                  If iostreams are in fact faster on your platform, it would be useful to know what that platform is. The reason that iostreams tend to be a lot slower than printf is that iostreams use a huge number of function calls, including a lot of virtual function calls (an iostream doesn't do anything by itself, it forwards everything to a streambuf virtual function). That's very useful if you have a need to redirect the streams, but wasteful if you aren't using it.

                                  – Ben Voigt
                                  May 22 '18 at 2:27













                                  Okay I don't really know what platform you refer to, but I am using Win 10, Intel Celeron CPU G3900, 4 gigs Ram, CPP 14. You should remember CPP evolves every three years. A lot of function calls are compiled away.

                                  – Javene CPP McGowan
                                  May 26 '18 at 3:58





                                  Okay I don't really know what platform you refer to, but I am using Win 10, Intel Celeron CPU G3900, 4 gigs Ram, CPP 14. You should remember CPP evolves every three years. A lot of function calls are compiled away.

                                  – Javene CPP McGowan
                                  May 26 '18 at 3:58













                                  Which compiler and which C++ standard library are very important details of the platform... the CPU (which you mentioned, and also contains cache information) and the RAM (which you mentioned only size but not clock rate, DDR generation, number of channels, or latency) are less important in this case but can sometimes have a big effect on performance and optimization.

                                  – Ben Voigt
                                  May 30 '18 at 3:19





                                  Which compiler and which C++ standard library are very important details of the platform... the CPU (which you mentioned, and also contains cache information) and the RAM (which you mentioned only size but not clock rate, DDR generation, number of channels, or latency) are less important in this case but can sometimes have a big effect on performance and optimization.

                                  – Ben Voigt
                                  May 30 '18 at 3:19











                                  0














                                  I tried this test on my laptop, running Windows 10, WSL Ubuntu, CLion 2018, GCC. No optimization:



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



                                  Results:



                                  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)


                                  TL;DR: Using std::ios_base::sync_with_stdio(false) and std::cin.tie(nullptr) brings both functions to almost the same stand.






                                  share|improve this answer



























                                    0














                                    I tried this test on my laptop, running Windows 10, WSL Ubuntu, CLion 2018, GCC. No optimization:



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



                                    Results:



                                    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)


                                    TL;DR: Using std::ios_base::sync_with_stdio(false) and std::cin.tie(nullptr) brings both functions to almost the same stand.






                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      I tried this test on my laptop, running Windows 10, WSL Ubuntu, CLion 2018, GCC. No optimization:



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



                                      Results:



                                      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)


                                      TL;DR: Using std::ios_base::sync_with_stdio(false) and std::cin.tie(nullptr) brings both functions to almost the same stand.






                                      share|improve this answer













                                      I tried this test on my laptop, running Windows 10, WSL Ubuntu, CLion 2018, GCC. No optimization:



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



                                      Results:



                                      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)


                                      TL;DR: Using std::ios_base::sync_with_stdio(false) and std::cin.tie(nullptr) brings both functions to almost the same stand.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 23 at 22:00









                                      AKJAKJ

                                      6710




                                      6710



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12044357%2fprintf-more-than-5-times-faster-than-stdcout%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript