正在加载视频...

视频加载失败

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

914,236 次观看 • 3 个月前 •via X (Twitter)

41 条评论

🇺🇸Eric S - America ONLY 🇺🇸 的头像
🇺🇸Eric S - America ONLY 🇺🇸3 个月前

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

Lxyv 的头像
Lxyv3 个月前

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 的头像
Daedalus3 个月前

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 的头像
Iluvalar3 个月前

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

Rango 的头像
Rango3 个月前

Stupid engagement bait for weak programmers.

Aditya 𖢻 Onchain 的头像
Aditya 𖢻 Onchain3 个月前

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 的头像
Lxyv3 个月前

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

Niraj 的头像
Niraj3 个月前

This means nothing without also posting the source code used.

Lxyv 的头像
Lxyv3 个月前

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 的头像
AIRobin3 个月前

only 0.2 sec with python here

Appreciator 的头像
Appreciator3 个月前

Now do machine language.

Ruud Harmsen 的头像
Ruud Harmsen3 个月前

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 的头像
Caveman2ez3 个月前

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

Hbg 的头像
Hbg3 个月前

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

Svetovid 的头像
Svetovid3 个月前

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

Lxyv 的头像
Lxyv3 个月前

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 的头像
Major3 个月前

@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 的头像
Svetovid3 个月前

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

sable 的头像
sable3 个月前

are we serious still doing this? in 2026?

TechX 的头像
TechX3 个月前

🐍 is still better for ML

矢部っち 的头像
矢部っち3 个月前

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

sxr!ptr 的头像
sxr!ptr3 个月前

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

Muthu / முத்து 的头像
Muthu / முத்து3 个月前

w/o seeing the code this post is useless

all goroutines are asleep 的头像
all goroutines are asleep3 个月前

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 的头像
Magnus Jørgensen3 个月前

I got even larger difference.

Pedro 的头像
Pedro3 个月前

it's the comparison between monster and baby chicken!

Alexander 的头像
Alexander3 个月前

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

sreecharan 的头像
sreecharan3 个月前

how about rust ?

Jadon | Jadox Engine 的头像
Jadon | Jadox Engine3 个月前

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 的头像
Ayhan Sipahi3 个月前

😅

jeffnyalik 的头像
jeffnyalik3 个月前

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

Pol 的头像
Pol3 个月前

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 的头像
EA-18G Growler Enjoyer3 个月前

Delete your account

解約リスナー 的头像
解約リスナー3 个月前

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

Warsemaphore3 的头像
Warsemaphore33 个月前

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 🇺🇸 的头像
Ordinary Beaver 🇺🇸3 个月前

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

Jay 的头像
Jay3 个月前

There used for different things so this is pointless

Decrypt 的头像
Decrypt3 个月前

@grok compare avec rust

ArtClark 的头像
ArtClark3 个月前

#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 的头像
honeybunny3 个月前

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 的头像
Collins3 个月前

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

相关视频