Skip navigation
Speed comparison between C++ and Python for a program that counts to 1,000,000,000.
0:00 / 0:26

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

Lxyv

@wholyv4K followers

Visit on X
2,574 likesDownloadOriginal post

914,625 views

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

41 Comments

Comments from the original post on X (Twitter)

  • 🇺🇸Eric S - America ONLY 🇺🇸's profile picture

    @glugalug

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

    64 likes

  • Lxyv's profile picture

    @wholyv

    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.

    27 likes

  • Daedalus's profile picture

    @Daedalus_Sy

    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.

    55 likes

  • Iluvalar's profile picture

    @Iluvalar1

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

    47 likes

  • Rango's profile picture

    @matthew_meadows

    Stupid engagement bait for weak programmers.

    23 likes

  • Aditya 𖢻 Onchain's profile picture

    @adi_verse1

    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

    18 likes

  • Lxyv's profile picture

    @wholyv

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

    9 likes

  • Niraj's profile picture

    @0xKNiraj

    This means nothing without also posting the source code used.

    17 likes

  • Lxyv's profile picture

    @wholyv

    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)

    26 likes

  • AIRobin's profile picture

    @AIRobin_net

    only 0.2 sec with python here

    14 likes

  • Appreciator's profile picture

    @AppreciatorWay

    Now do machine language.

    8 likes

  • Ruud Harmsen's profile picture

    @rudharcom

    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.

    6 likes

  • Caveman2ez's profile picture

    @caveman2ez

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

    6 likes

  • Hbg's profile picture

    @HbgHbg1010

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

    6 likes

  • Svetovid's profile picture

    @TopCurlsoid

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

    5 likes

  • Lxyv's profile picture

    @wholyv

    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.

    31 likes

  • Major's profile picture

    @Apar_maker

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

    8 likes

  • Svetovid's profile picture

    @TopCurlsoid

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

    3 likes

  • sable's profile picture

    @sable_apathy

    are we serious still doing this? in 2026?

    5 likes

  • TechX's profile picture

    @TechWeaponX

    🐍 is still better for ML

    5 likes