One of my colleague at work told me that Python code is faster than C++ code and then showed this topic as an example to prove his point. It is now obvious from other answers that what is wrong with the C++ code posted in the question. I still would like to summarize my benchmarks which I did in order to show him how fast a good C++ code can be!
There are two problems with the original C++ code:
It uses
std::endlto print a newline in each iteration. That is a very bad idea becausestd::endldoes more stuff than simply printing a newline — it also forces the stream to flush the buffer accumulated so far; flushing is an expensive operation as it has to deal with hardware – the output device. So the first fix is this: if you want to print a newline, just use'\n'.The second problem is less obvious as it is not seen in the code. It is in the design of C++ streams. By default, C++ streams are synchronized to the C streams after each input and output operation so that your application could mix
std::coutandstd::printf, andstd::cinandstd::scanfwithout any problem. This feature (yes, it is a feature) is not needed in this case so we can disable this, as it has a little runtime overhead (that is not a problem; it doesn't make C++ bad; it is simply a price for the feature). So the second fix is this:std::cout::sync_with_stdio(false);
And here is the final optimized code:
#include <iostream>
int main()
{
std::ios_base::sync_with_stdio(false);
int x = 0;
while ( x != 1000000 )
{
++x;
std::cout << x << '\n';
}
}
And compile this with -O3 flags and run (and measure) as:
$ g++ benchmark.cpp -O3 #compilation
$ time ./a.out #run
//..
real 0m32.175s
user 0m0.088s
sys 0m0.396s
And run and measure python code (posted in the question):
$ time ./benchmark.py
//...
real 0m35.714s
user 0m3.048s
sys 0m4.456s
The user and sys time tell us which one is fast, and by what order.
Hope that helps you to remove your doubts. :-)
Answer from Sarfaraz Nawaz on Stack OverflowOne of my colleague at work told me that Python code is faster than C++ code and then showed this topic as an example to prove his point. It is now obvious from other answers that what is wrong with the C++ code posted in the question. I still would like to summarize my benchmarks which I did in order to show him how fast a good C++ code can be!
There are two problems with the original C++ code:
It uses
std::endlto print a newline in each iteration. That is a very bad idea becausestd::endldoes more stuff than simply printing a newline — it also forces the stream to flush the buffer accumulated so far; flushing is an expensive operation as it has to deal with hardware – the output device. So the first fix is this: if you want to print a newline, just use'\n'.The second problem is less obvious as it is not seen in the code. It is in the design of C++ streams. By default, C++ streams are synchronized to the C streams after each input and output operation so that your application could mix
std::coutandstd::printf, andstd::cinandstd::scanfwithout any problem. This feature (yes, it is a feature) is not needed in this case so we can disable this, as it has a little runtime overhead (that is not a problem; it doesn't make C++ bad; it is simply a price for the feature). So the second fix is this:std::cout::sync_with_stdio(false);
And here is the final optimized code:
#include <iostream>
int main()
{
std::ios_base::sync_with_stdio(false);
int x = 0;
while ( x != 1000000 )
{
++x;
std::cout << x << '\n';
}
}
And compile this with -O3 flags and run (and measure) as:
$ g++ benchmark.cpp -O3 #compilation
$ time ./a.out #run
//..
real 0m32.175s
user 0m0.088s
sys 0m0.396s
And run and measure python code (posted in the question):
$ time ./benchmark.py
//...
real 0m35.714s
user 0m3.048s
sys 0m4.456s
The user and sys time tell us which one is fast, and by what order.
Hope that helps you to remove your doubts. :-)
There isn't anything obvious here. Since Python's written in C, it must use something like printf to implement print. C++ I/O Streams, like cout, are usually implemented in a way that's much slower than printf. If you want to put C++ on a better footing, you can try changing to:
#include <cstdio>
int main()
{
int x=0;
while(x!=1000000)
{
++x;
std::printf("%d\n", x);
}
return 0;
}
I did change to using ++x instead of x++. Years ago people thought that this was a worthwhile "optimization." I will have a heart attack if that change makes any difference in your program's performance (OTOH, I am positive that using std::printf will make a huge difference in runtime performance). Instead, I made the change simply because you aren't paying attention to what the value of x was before you incremented it, so I think it's useful to say that in code.
Python 3.14 Will be Faster than C++ (Not really)
Python 3.14 Is Here. How Fast Is It?
Python 3.14 is here. How fast is it?
Can python be faster than c++ in future? Any chance!
Videos
Guys, I am very interested in this question. Does anyone have an answer to this? Because I have good knowledge about python. also I am more interested in robotics. Therefore, speed is important in robotics. That's why I asked this question.