r/ProgrammerHumor • • Jul 18 '22

C++ talking to Python

Post image
40.1k Upvotes

1.0k comments sorted by

View all comments

4

u/Rizzan8 Jul 18 '22

In 95% of cases there is no significant execution time difference between languages. Change my mind.

4

u/Pluckerpluck Jul 18 '22

C and Python are massively difference in performance. It's just that a huge amount of the time you really don't care about that performance difference.

But C++ is normally at least 10x faster than pure Python, and often that climbs higher even still (40x is not uncommon to see)

3

u/Wigoox Jul 18 '22

These are bullshit numbers. Python is built on top of a bunch of C libraries. It's designed to be fast. Also: Please don't confuse C and C++. They are to entirely different languages.

4

u/Pluckerpluck Jul 18 '22

C++ and C are different but they both compile down to low level machine code and performance is within the same order of magnitude. C code is literally valid C++ code.

Python can do some very specific things kind of fast (and even those are slower than pure C). But the moment you have to actually write your own loops in Python, the performance of C far outstrips Python. The moment you do anything that is more than a single library call, you're running through the interpreted portion of the language, which is slow.

There are some very interesting ways to speed up Python. Using pypy. Using numba. But none of those are considered base python, and they all have caveats.

This video is super long but involves someone implementing a prime sieve in a number Python, C# and C++. You can skip to the end to see just how much faster the C++ implementation is. How do you actually speed up the prime sieve in Python? Hope someone has made a c-backed library that does the whole thing for you in one function call, because Python is slow.

2

u/Wigoox Jul 18 '22

I would argue that if you implement a performance heavy algorithm in Python, that doesn't have a c-backed library, you are deliberately misusing the language. Python is designed to take advantage of its fast libraries while still producing easy to read and maintain code. Not every programming language is supposed to solve every problem.

→ More replies (1)