Video wird geladen...

Video konnte nicht geladen werden

Zur Startseite

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

914,236 Aufrufe • vor 3 Monaten •via X (Twitter)

41 Kommentare

Profilbild von 🇺🇸Eric S - America ONLY 🇺🇸
🇺🇸Eric S - America ONLY 🇺🇸vor 3 Monaten

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

Profilbild von Lxyv
Lxyvvor 3 Monaten

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.

Profilbild von Daedalus
Daedalusvor 3 Monaten

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.

Profilbild von Iluvalar
Iluvalarvor 3 Monaten

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

Profilbild von Rango
Rangovor 3 Monaten

Stupid engagement bait for weak programmers.

Profilbild von Aditya 𖢻 Onchain
Aditya 𖢻 Onchainvor 3 Monaten

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

Profilbild von Lxyv
Lxyvvor 3 Monaten

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

Profilbild von Niraj
Nirajvor 3 Monaten

This means nothing without also posting the source code used.

Profilbild von Lxyv
Lxyvvor 3 Monaten

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)

Profilbild von AIRobin
AIRobinvor 3 Monaten

only 0.2 sec with python here

Profilbild von Appreciator
Appreciatorvor 3 Monaten

Now do machine language.

Profilbild von Ruud Harmsen
Ruud Harmsenvor 3 Monaten

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.

Profilbild von Caveman2ez
Caveman2ezvor 3 Monaten

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

Profilbild von Hbg
Hbgvor 3 Monaten

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

Profilbild von Svetovid
Svetovidvor 3 Monaten

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

Profilbild von Lxyv
Lxyvvor 3 Monaten

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.

Profilbild von Major
Majorvor 3 Monaten

@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?

Profilbild von Svetovid
Svetovidvor 3 Monaten

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

Profilbild von sable
sablevor 3 Monaten

are we serious still doing this? in 2026?

Profilbild von TechX
TechXvor 3 Monaten

🐍 is still better for ML

Profilbild von 矢部っち
矢部っちvor 3 Monaten

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

Profilbild von sxr!ptr
sxr!ptrvor 3 Monaten

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

Profilbild von Muthu / முத்து
Muthu / முத்துvor 3 Monaten

w/o seeing the code this post is useless

Profilbild von all goroutines are asleep
all goroutines are asleepvor 3 Monaten

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

Profilbild von Magnus Jørgensen
Magnus Jørgensenvor 3 Monaten

I got even larger difference.

Profilbild von Pedro
Pedrovor 3 Monaten

it's the comparison between monster and baby chicken!

Profilbild von Alexander
Alexandervor 3 Monaten

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

Profilbild von sreecharan
sreecharanvor 3 Monaten

how about rust ?

Profilbild von Jadon | Jadox Engine
Jadon | Jadox Enginevor 3 Monaten

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

Profilbild von Ayhan Sipahi
Ayhan Sipahivor 3 Monaten

😅

Profilbild von jeffnyalik
jeffnyalikvor 3 Monaten

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

Profilbild von Pol
Polvor 3 Monaten

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

Profilbild von EA-18G Growler Enjoyer
EA-18G Growler Enjoyervor 3 Monaten

Delete your account

Profilbild von 解約リスナー
解約リスナーvor 3 Monaten

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

Profilbild von Warsemaphore3
Warsemaphore3vor 3 Monaten

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.

Profilbild von Ordinary Beaver 🇺🇸
Ordinary Beaver 🇺🇸vor 3 Monaten

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

Profilbild von Jay
Jayvor 3 Monaten

There used for different things so this is pointless

Profilbild von Decrypt
Decryptvor 3 Monaten

@grok compare avec rust

Profilbild von ArtClark
ArtClarkvor 3 Monaten

#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

Profilbild von honeybunny
honeybunnyvor 3 Monaten

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

Profilbild von Collins
Collinsvor 3 Monaten

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

Ähnliche Videos