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. :-)

Answer from Sarfaraz Nawaz on Stack Overflow
🌐
Datatas
datatas.com › is-python-3-14-faster-than-c
Is Python 3.14 faster than C++? - Datatas
July 4, 2023 - While C++ is faster and more efficient than Python, Python is easier to learn and use, and has a large number of libraries and modules that can be used to speed up the development process.
Top answer
1 of 5
54

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. :-)

2 of 5
20

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.

Discussions

Python 3.14 Will be Faster than C++ (Not really)
On LinkedIn I have already seen a couple of post of people testing the new version and their results were stunning. But the best method to get a feeling on how fast Python 3.11 truly is, is to run the tests yourself. https://towardsdatascience.com/python-3-14-will-be-faster-than-c-a97edd01... More on democraticunderground.com
🌐 democraticunderground.com
March 1, 2023
Python 3.14 Is Here. How Fast Is It?
PyPy is one of the weirdest projects ever. Let’s try to make a JIT compiler We tried, but for these features it was not possible, so let’s call what we could achieve as RPython (restricted Python) and call it a day What if we make a Python interpreter in RPython? It freaking works 🤣 More on reddit.com
🌐 r/programming
128
272
October 8, 2025
Python 3.14 is here. How fast is it?
Thank you for the work you put in · I'm now a SWE with just a marketing degree More on news.ycombinator.com
🌐 news.ycombinator.com
557
746
October 15, 2025
Can python be faster than c++ in future? Any chance!
The short answer is no. Python is garbage collected and dynamic, while C++ is not. That being said, with the advancement of technology and the improvement in JIT compilers, it's more likely that the difference will become less apparent over time, but it will never be by an amount significant enough that we'll start using Python over C++ for performance critical applications and programs. More on reddit.com
🌐 r/learnpython
107
69
August 13, 2022
🌐
LinkedIn
linkedin.com › posts › towards-data-science_python-314-will-be-faster-than-c-activity-6975626930846937088-hU0V
Towards Data Science on LinkedIn: Python 3.14 will be faster than C++ | 36 comments
September 14, 2022 - Which is good enough for most people. If you really need that speed, use Cython or Pybind11. ... His article said Python 3.14 would be faster.
🌐
Democratic Underground
democraticunderground.com › 122883554
Python 3.14 Will be Faster than C++ (Not really) - Democratic Underground Forums
March 1, 2023 - I looked at the article, the author does a number of tests with different versions of python to show that the interpreter (python is interpreted, not compiled) is getting faster and faster with each version. Then the author PROJECTS that future versions of python will continue to exhibit the same increase of speed compared to the previous version...
🌐
Reddit
reddit.com › r/programming › python 3.14 is here. how fast is it?
r/programming on Reddit: Python 3.14 Is Here. How Fast Is It?
October 8, 2025 - Python 3.14, due out later this year, is set to receive a new type of interpreter that can boost performance by up to 30% with no changes to existing code. 👨‍💻 ... Python 3.14 is here...
🌐
Medium
medium.com › @hieutrantrung.it › pythons-performance-revolution-how-3-11-made-speed-a-priority-4cdeee59c349
Python’s Performance Revolution: How 3.11+ Made Speed a Priority | by Trung Hiếu Trần | Medium
January 5, 2026 - Python 3.14, released in October ... languages in raw execution speed, the gap has narrowed significantly: Typical Benchmark Performance (Relative to C):[¹³] ... The performance improvements in Python 3.11+ represent more than ...
Find elsewhere
🌐
LinkedIn
linkedin.com › posts › eran-shlomo-0259b54_python-314-will-be-faster-than-c-activity-6979500031565262848-cELm
Eran Shlomo on LinkedIn: Python 3.14 will be faster than C++ | 17 comments
September 24, 2022 - When did programmers start doing click baits ? 3.14 wont be faster, python by design is slower than C++. while 3.11 is indeed much faster and worth upgrading python troubles were never the single thread performance but multithread blocks on GIL.
🌐
Medium
medium.com › data-science › python-3-14-will-be-faster-than-c-a97edd01d65d
Python 3.14 Will be Faster than C++ | by Dennis Bakhuis | TDS Archive | Medium
April 4, 2023 - Python 3.14 Will be Faster than C++ Benchmarking the new and impressive Python 3.11 Python is one of the most used scripting languages in data science (DS) and machine learning (ML). According to …
🌐
Hacker News
news.ycombinator.com › item
Python 3.14 is here. How fast is it? | Hacker News
October 15, 2025 - Thank you for the work you put in · I'm now a SWE with just a marketing degree
🌐
Quora
quora.com › How-is-Python-able-to-be-faster-than-C-despite-being-an-interpreted-language-and-using-a-virtual-machine
How is Python able to be faster than C++ despite being an interpreted language and using a virtual machine? - Quora
Well written Python code run in the cPython interpreter is generally several times slower to execute than similarly well written compiled and compiler-optimized c++. You can run roughly the same Pyth...
🌐
Quora
quora.com › I-ve-heard-that-the-Python-programming-language-has-become-faster-over-the-years-Is-it-true-that-Python-s-performance-has-greatly-improved-compared-to-its-older-versions-and-would-you-recommend-it-for-building
I’ve heard that the Python programming language has become faster over the years. Is it true that Python’s performance has greatly improv...
A programming language is a specification and, as such, cannot become faster. It puts some constraints on what is possible and the dynamic typing is one of the main problems concerning performance, because you need to check the types of the values all the time. However, tooling tends to improve in various ways. There are versions of tooling that performs just-in-time compilation · [1] that is better than the vanilla Python tooling. In Python 3.14, they finally got rid of the Global Interface Lock and can run true multithreading, which will improve the situation.
🌐
Miguel Grinberg
blog.miguelgrinberg.com › post › is-python-really-that-slow
Is Python Really That Slow? - miguelgrinberg.com
Below you can see the results of running this second script under the same conditions as the first: Overall there isn't a big difference in the results of this test. The 3.10 version was a bit slower than 3.8 and 3.9 in the first test, while ...
🌐
Python.org
discuss.python.org › python help
Is today's python faster than 20 years ago's Fortran or C? - Python Help - Discussions on Python.org
January 19, 2024 - It is said that python is 100 times slower than C. But is the hardware improvement more than 100 times in the past 20 years? If so, today’s python would be comparable with 20 years ago’s C. That is still enough for many jobs.
🌐
Hacker News
news.ycombinator.com › item
Performance of the Python 3.14 tail-call interpreter | Hacker News
March 13, 2025 - First, I want to say thank you to Nelson for spending almost a month to get to the root of this issue · Secondly, I want to say I'm extremely embarrassed and sorry that I made such a huge oversight. I, and probably the rest of the CPython team did not expect the compiler we were using for ...
🌐
InfoWorld
infoworld.com › home › software development › programming languages
A new interpreter in Python 3.14 delivers a free speed boost | InfoWorld
March 10, 2025 - The new interpreter will run Python programs as much as 5% faster, with no changes to existing code required. A beta of Python 3.14 is due in May.
🌐
EuroPython 2025
ep2025.europython.eu › session › performance-improvements-in-3-14-and-maybe-3-15
Performance improvements in 3.14 and maybe 3.15
After making a splash with major performance improvements in Python 3.11, things were relatively quiet for 3.12 and 3.13. CPython got faster, but not by much. For 3.14, performance is up again. CPython 3.14 will be quite a bit faster than 3.13 and, ...
🌐
Lost
lost.co.nz › articles › sixteen-years-of-python-performance
Sixteen Years of Python Performance - Lost
I build and benchmarked 17 versions of Python against each other so that you don't have to. Let's compare the performance of every Python version from 2.6 all the way to the current 3.14 alpha release. Spoiler, it's going great!