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;
#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 fflush
ing 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
|
show 4 more comments
#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 fflush
ing 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
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 thatstd::cout << "Hello, World! (" << i << ")" << std::endl;
is 4 function calls, whileprintf
is only one. Try doingprintf
5 times, like withcout
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
|
show 4 more comments
#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 fflush
ing 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
#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 fflush
ing 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
c++ performance printf cout
asked Aug 20 '12 at 20:06
![](https://i.stack.imgur.com/RDsrQ.jpg?s=32&g=1)
![](https://i.stack.imgur.com/RDsrQ.jpg?s=32&g=1)
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 thatstd::cout << "Hello, World! (" << i << ")" << std::endl;
is 4 function calls, whileprintf
is only one. Try doingprintf
5 times, like withcout
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
|
show 4 more comments
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 thatstd::cout << "Hello, World! (" << i << ")" << std::endl;
is 4 function calls, whileprintf
is only one. Try doingprintf
5 times, like withcout
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
|
show 4 more comments
6 Answers
6
active
oldest
votes
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.
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 thanprintf
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
|
show 1 more comment
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.
add a comment |
use
cout << "n";
to prevent buffering. much faster
add a comment |
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.
add a comment |
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.
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 astreambuf
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
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 thanprintf
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
|
show 1 more comment
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.
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 thanprintf
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
|
show 1 more comment
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.
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.
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 thanprintf
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
|
show 1 more comment
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 thanprintf
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
|
show 1 more comment
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.
add a comment |
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.
add a comment |
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.
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.
answered Aug 20 '12 at 21:33
Martin YorkMartin York
202k67271490
202k67271490
add a comment |
add a comment |
use
cout << "n";
to prevent buffering. much faster
add a comment |
use
cout << "n";
to prevent buffering. much faster
add a comment |
use
cout << "n";
to prevent buffering. much faster
use
cout << "n";
to prevent buffering. much faster
answered Apr 28 '17 at 14:37
JasonJason
736810
736810
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Apr 28 '17 at 14:59
Tianze.ChenTianze.Chen
291
291
add a comment |
add a comment |
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.
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 astreambuf
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
add a comment |
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.
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 astreambuf
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
add a comment |
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.
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.
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 astreambuf
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
add a comment |
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 astreambuf
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 23 at 22:00
AKJAKJ
6710
6710
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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, whileprintf
is only one. Try doingprintf
5 times, like withcout
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