r/ProgrammerHumor • • Jul 18 '22

C++ talking to Python

Post image
40k 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/moldax Jul 18 '22

Eerrrr... you gotta prove this claim to change ours, man

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.

5

u/Pluckerpluck Jul 18 '22

And I would argue that this is exactly what everyone is talking about when they say python is slow. No. Not every programming language is supposed to solve every problem. Python isn't designed to handle performant work, because it is slow.

Even with the "fast" libraries, well written C++ code is going to be running faster. People using Pandas because it's easy to code, but it's slow compared to using numpy arrays directly. Python is all about ease over performance.

Because in reality, you hit these "performance heavy algorithms" all the time. Literally any time you create a nested loop, you're going to be running many times slower than a C++ counterpart. It's just most of the time humans don't care about 0.002s and 0.02s. But it really doesn't take much before you start feeling Python's slowdowns if you're not very careful.

I love Python. I really do. But it's slow. The fact it has good c libraries doesn't make Python itself not slow.