Загрузка видео...

Не удалось загрузить видео

На главную

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

Похожие видео