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::endl
to print a newline in each iteration. That is a very bad idea because std::endl
does 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::cout
and std::printf
, and std::cin
and std::scanf
without 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. :-)