Python vs C++ Speed Comparison

I ran the same code on both languages and here is the result.

Halim Shams
4 min readAug 9, 2024
An Article by — Halim Shams

There have been a lot complains about the C++ programming language due to the fact that collages or universities should stop teaching C++ to their CS field students as it’s an old language and has no use and bla bla bla.

On the other hand, there have been programmers consistently recommending Python for the new programmers as it’s easy to learn, powerful, and in-demand, and so on and so forth.

So, in this article, I have tried to write the same code in both of these programming languages and ran it so that you see the entire comparison — the code and speed — between these two languages in one go.

To find out about the speed and the code comparison between these two programming languages, I’ve wrote a pretty basic and simple program to count from 0 to 1000,000,000 — a billion in case you don’t know.

Python

This is how you can write such a program in Python programming language:

counter = 0
while (counter < 1000000000):
counter+=1

print(counter)

It’s a simple “while loop”, it’s possible to achieve the same goal using “for loop” which take yet more less space and code, but in this comparison I love to use “while loop” because… I want to and there’s no difference between “while loop” and “for loop”.

Now let’s see the amount of time it took to reach to a billion and print it out.

And here you go, it’s the actual screenshot of the terminal showcasing the entire time it took to print 1000,000,000.

Python program result (terminal)

For Python, it took 1 minute, 19 seconds and 214 milli seconds. Simply put, 1m.19s to finally reach a billion and print it to the terminal.

C++

And this is literally how you can write a similar program in C++ programming language:

#include <iostream>

int main() {
size_t counter = 0;
while (counter < 1000000000)
counter++;
std::cout << counter << std::endl;
}

Again it’s a simple “while loop” that loops through all the numbers until it reaches to a billion (1000,000,000) and print out that billion.

Can you guess the amount of time it took for C++ to reach a billion? or can you at least guess which programming language took the longest?

Exclusive content for passionate programmers! Subscribe to our newsletter for latest in tech, tips, hacks, and advice that will transform your coding journey.

Subscribe

Alright, let’s reveal the result.

This is how much it took for C++ to reach 1000,000,000:

C++ program result (terminal)

It took 21ms (milli seconds) let alone a second, 21 MILLI SECONDS for C++ to loop through one billion!

It will certainly be more confusing for the new programmer who have recently got into the programming industry, and heard that Python is the fastest programming language to start with which almost totally untrue.

While it’s true that Python has become a superhero as with Python you can do whatever you want, which means it’s a general-purpose programming language compared to C++.

It doesn’t mean that if you’re a Python programmer, or are currently learning Python, should abandon it for good. Python has got its PROs and CONs; C++ has got its PROs and CONs. So keep learning and don’t be a jerk to jump straight to C++ after discovering this.

This has been all it for this article, make sure you share it with other programmers as well if you found it insightful!

Don’t forget to subscribe to my exclusive newsletter for My Audience: 👇

Subscribe Now!

Subscribe to my newsletter now! It’s totally FREE!

— You can follow me on X (formerly Twitter) or LinkedIn as well, where I’ll share short and incredible stuffs out there, so don’t miss those. 🚀

Buy me a Coffee

--

--

Halim Shams

I Write about Programming and All the Related Content 🚀 I'm a Self-Taught Full-Stack Developer 💛