Loading video...

Video Failed to Load

Go Home

Speed comparison between C++ and Python for a program that counts to 1,000,000,000.

914,236 views • 3 months ago •via X (Twitter)

41 Comments

🇺🇸Eric S - America ONLY 🇺🇸's profile picture
🇺🇸Eric S - America ONLY 🇺🇸3 months ago

2.4s? Was your C++ binary writing all those numbers to the console?

Lxyv's profile picture
Lxyv3 months ago

Nope, not at the console. In both programs (in C++ and Python), only a for loop was established, and the final output, i.e., 1 billion, was outputted to the console.

Daedalus's profile picture
Daedalus3 months ago

I’m too old to put up with that nonsense anymore. I’d rather perfect the programs appearance and logic in Python then convert and optimize portions or the entire program later. Speed during design and development is a metric that matters far more than execution speed during design and development phase. You can figure out if a program and workflow will work with real users MUCH FASTER with a language like Python. Anything can be optimized and/or rebased later.

Iluvalar's profile picture
Iluvalar3 months ago

You clearly made a mistake and did a poor job in your python code.

Rango's profile picture
Rango3 months ago

Stupid engagement bait for weak programmers.

Aditya 𖢻 Onchain's profile picture
Aditya 𖢻 Onchain3 months ago

If I'm not wrong it mostly shows C++ compiler optmizn deleting empty loops, vs .py intrptr overhead. Running python through PyPy or NumPy narrows that gap instantly. Great example of compiled vs. intrptr design though

Lxyv's profile picture
Lxyv3 months ago

@Adi_Verse_ performance critical parts are implemented in C in numpy.

Niraj's profile picture
Niraj3 months ago

This means nothing without also posting the source code used.

Lxyv's profile picture
Lxyv3 months ago

Code used (C++): int main() { size_t n = 0; while (n < 1'000'000'000) n++; std::cout << n << std::endl; } Code used (Python): n = 0 while (n < 1000000000): n += 1 print(n)

AIRobin's profile picture
AIRobin3 months ago

only 0.2 sec with python here

Appreciator's profile picture
Appreciator3 months ago

Now do machine language.

Ruud Harmsen's profile picture
Ruud Harmsen3 months ago

The optimising phase of the C compiler sees that the program produces no output, so all the counting can be optimised out, or at least replaced by just setting the variable to the end value.

Caveman2ez's profile picture
Caveman2ez3 months ago

Complines down to; C++ --> Machine Code Python --> C++ --> Machine Code

Hbg's profile picture
Hbg3 months ago

Show the code because that looks just like compiler optimization. C loops should be faster regardless but like not THAT fast

Svetovid's profile picture
Svetovid3 months ago

Apples and oranges. ASM will have C++ eating its dust.

Lxyv's profile picture
Lxyv3 months ago

I code in ASM. difference of speed between C and ASM is not even noticeable. even tho asm is obviously faster. to humans, gap between <1s and <2s is irrelevant.

Major's profile picture
Major3 months ago

@TopCurlsoid People who don’t see real value behind the different purposes of languages they use are easily measuring the things they understand; speed, time or delay. But if that is necessary to have just a loop like that, why don’t you write it as asm function and call it from Python prog?

Svetovid's profile picture
Svetovid3 months ago

@wholyv Precisely. And if you need to optimize coefficient's of a horner's scheme, use Python, not C or ASM.

sable's profile picture
sable3 months ago

are we serious still doing this? in 2026?

TechX's profile picture
TechX3 months ago

🐍 is still better for ML

矢部っち's profile picture
矢部っち3 months ago

@grok 何か胡散臭いんだけど…

sxr!ptr's profile picture
sxr!ptr3 months ago

It's bs to compare a compiled language and interpreted one

Muthu / முத்து's profile picture
Muthu / முத்து3 months ago

w/o seeing the code this post is useless

all goroutines are asleep's profile picture
all goroutines are asleep3 months ago

you need to compile it with -O0 to avoid constant propagataion and loop unrolling. C++ doesn't actually run the loop if the optimizations elliminate it completely before generating assembly code. rookie mistake, terrible example

Magnus Jørgensen's profile picture
Magnus Jørgensen3 months ago

I got even larger difference.

Pedro's profile picture
Pedro3 months ago

it's the comparison between monster and baby chicken!

Alexander's profile picture
Alexander3 months ago

Different use cases for these languages. Plus your test case is silly because it serves no real purpose

sreecharan's profile picture
sreecharan3 months ago

how about rust ?

Jadon | Jadox Engine's profile picture
Jadon | Jadox Engine3 months ago

C++ makes it harder for you to shoot yourself in the foot but once you do it it blows away your entire leg

Ayhan Sipahi's profile picture
Ayhan Sipahi3 months ago

😅

jeffnyalik's profile picture
jeffnyalik3 months ago

Interpreted Vs compiled of course the interpreted one will be slow.

Pol's profile picture
Pol3 months ago

Given the post AND the comments, I understand most people don't know the differences in execution time magnitude between compiled and interpreted code, and why that doesn't really matters when most of your computation is done by a compiled lib...

EA-18G Growler Enjoyer's profile picture
EA-18G Growler Enjoyer3 months ago

Delete your account

解約リスナー's profile picture
解約リスナー3 months ago

コンパイル言語なんだから当たり前の話だと思いますが・・・

Warsemaphore3's profile picture
Warsemaphore33 months ago

That's not what Python is design for. Python is a glue code, when it needs speed it calls C++ libraries. It's strength is how easy it is to learn and use. Each language has its pros and cons, use the right tool for the job.

Ordinary Beaver 🇺🇸's profile picture
Ordinary Beaver 🇺🇸3 months ago

The real trick is to call the C++ from python, and use python for everything that is IO bound instead of CPU bound.

Jay's profile picture
Jay3 months ago

There used for different things so this is pointless

Decrypt's profile picture
Decrypt3 months ago

@grok compare avec rust

ArtClark's profile picture
ArtClark3 months ago

#include <stdio.h> #include <time.h> int main(){ volatile int count=0;clock_t start=clock(); for(int i=1;i<=1000000;i++)count=i;clock_t end=clock();double time_ms=1000.0*(double)(end-start)/CLOCKS_PER_SEC; printf("%.3f ms\n",time_ms,count); return 0; } 3.000 ms

honeybunny's profile picture
honeybunny3 months ago

It does not matter anymore. Programming language were a filter for binary so us humans can work with machine. With AI, this becomes completely obey

Collins's profile picture
Collins3 months ago

Python is slower than C++ in raw computation, but its real world speed comes from how quickly you can build and iterate making it faster for development even if it's slower for execution. E.g on Msc project doc retriever used python later embedded c for more faster retriever

Related Videos