r/Python • u/Anthonypjshaw • Jul 16 '18
Why is Python so slow?
https://medium.com/@anthonypjshaw/why-is-python-so-slow-e5074b6fe55b12
u/lambdaq django n' shit Jul 16 '18
tl;dr
GIL is slow because obviously everyone runs Django using native threading.
no JIT/AOT and it's a must have for fast languages
Python has no static type so guessing the type during runtime is incredibly hard.
I think CPython is slow solely because it's never been a top priority in PSF.
9
Jul 16 '18
4. if you need to go fast there are better suited languages.
edit: welp, should've said 4.,
6
3
u/chchan Jul 16 '18
Python is easy to write and pretty clean to read but trades off some speed. If you need to go faster and don't want to rewrite everythin you can do several things:
use Cython which uses C
Numba which uses a JIT compiler
You can use apache thrift or libraries to send stuff to run in other languages.
1
Jul 16 '18
Can somebody explain the technical reasoning behind #1 above for somebody trying to understand what GIL has to do with Django and the native threading comment? (And are learning Python?)
3
u/lambdaq django n' shit Jul 16 '18 edited Jul 16 '18
GIL is only relevant when you are doing
single process
native (OS) threading
multi-core
CPU-bound task
It's very hard to find a problem hitting all of the four criteria
A typical (yet non-typical) example is a web server. If your server can only handle a single user at at time then it's gunna be problematic.
The naive solution would be using multi-threaded app to serve web pages. You can increase your thread pool as the user number grows.
However that's not how most modern web servers work. They either use multi-process, or pre-forked multi-process, each process has its own thread pool, or so called user-land threading. And let's be realistic, your cheap VPS/container may only have 1 or 2 cores. So GIL won't effect much. Besides in pre-fork model you can bind your worker process to each core (affinity) so GIL is again irrelevant.
Native threading (aka OS threading) can solve some problems but often you ended up solving it without threading in Python. For IO-bound tasks Python can handle multi-threading OK, for CPU-bound tasks, Python indeed have a serious problem with multi-core support because of GIL.
Then again, if you aim for CPU speed, why use CPython anyway?
tl;dr If you have a GIL problem you are doing it wrong.
1
1
u/jmoiron Jul 16 '18
It's very hard to find a problem hitting all of the four criteria
I see this all the time in Python threads, and I've seen it in Raymond Hettinger's talks. This is a post-facto justification.
It's hard to find applications that have these problems in Python because Python unsuitable for them, not because the problems are rare.
The truth is that they exist in nearly every project, but they're usually exported to other systems not written in Python. For lots of web applications, this is the database; for data intensive applications, it's spark + some combo of pandas and numpy.
2
u/lambdaq django n' shit Jul 16 '18
web applications are a solved problem. See uWSGI or Gunicorn.
database access is IO-bound, not CPU-bound.
Spark is also IO-bound. Lots of time is spent on serialization.
I remember Pandas2 was rewritten in Cython or something.
The truth is if your single threaded program is slow, multi-thread won't help either.
2
u/jmoiron Jul 17 '18
Problems requiring parallelism are exported to other tools written in other languages because Python supports it poorly. Often this comes at the added cost of serialization (which is CPU bound, generally). Python programmers don't experience this because these systems are not written in Python; of the systems I named, only one has significant Python code (Cython is not Python).
If a Python programmer wants to write this type of program, they will probably have to choose a different language. I can buy the idea that they should do so anyway ("if you aim for CPU speed, why use CPython anyway?"), and maybe that means it's true that CPU parallelism will not significantly change Python's strengths.
What I don't agree with is that these problems are rare; I think they occur in most projects. And to clarify, even if Python got 10x faster + cpu parallelization tomorrow, people would not be ditching these other systems; but what would change is that Python programmers would start writing them, and perhaps more tellingly users of other languages would use them.
It's the denial that this is common that lets people say "If your single threaded program is slow, multi-thread won't help either." with such confidence while also having said "for CPU-bound tasks, Python indeed has a serious problem". It can't be both.
1
u/lambdaq django n' shit Jul 18 '18
I've seen many game engines running hot 100% but only one core. Most of them are written in C++. Can you explain that?
1
Jul 16 '18
He mis-spoke. He meant processes not threads.
I think he's saying that using multiple processes for web is common, but other domains don't do it.
1
u/13steinj Jul 16 '18
I'm pretty sure Python doesn't bother guessing the type at runtime anyway-- the only time where this matters is doing an operation on two different types, such as string and numerical addition, but IIRC Python does
Keep type information on the object
Doesn't fail directly on type mismatch, but rather when the defined magic method for the operator doesn't support the other type.
1
u/stefantalpalaru Jul 16 '18
everyone runs Django using native threading
That's not threading. Those are separate processes.
no JIT/AOT and it's a must have for fast languages
Interpreted Lua is much faster.
I think CPython is slow solely because it's never been a top priority in PSF.
It's not that. Many design decisions make it very hard to optimise the interpreter. See https://code.google.com/archive/p/unladen-swallow/
1
u/lambdaq django n' shit Jul 16 '18
very hard to optimise the interpreter
Not really, CPython intentionally rejects many performance patches (Mark Shannon, Carl Shapiro, etc.), their reason is to maintain C code "clean".
Sarcastically, they accepted PEP-572.
2
u/stefantalpalaru Jul 16 '18
Not really
Yes, really. All attempts to significantly speed up CPython forks have failed. Even Google's - the one I linked and you did not bother to check out.
many performance patches (Mark Shannon, Carl Shapiro, etc.)
Where are they? I might add them to Tauthon.
1
u/lambdaq django n' shit Jul 16 '18
significantly speed up
That's the problem. Rome was not built in one day.
1
8
u/pmatti pmatti - mattip was taken Jul 16 '18
tl;dr Use PyPy if you want speed,
The GIL is a non-issue, or someone would have funded a GILless PyPy for $50,000 for poc and 100000 for the finished product [0].
Maybe I missed the point of the article. It does not even mention numba’s JIT, nor does it describe cython or pythran and when you would want to use them.
6
u/stefantalpalaru Jul 16 '18
Use PyPy if you want speed
Unless your program doesn't run long enough for the speed gains to overcome the startup, tracing and JIT-ing overhead.
1
1
u/lambdaq django n' shit Jul 16 '18
GILless pypy is easy if you don't care about Python compatibility. In fact GIL is overrated problem. There are fewer and fewer reasons that you should use native threading directly. Program your task based on some kind of M:N user-land thread like Coroutine fits much better in today's world.
2
u/Paddy3118 Jul 16 '18 edited Jul 16 '18
Python is obviously fast enough to fuel its continued growth.
Speed should be treated as an optimisation; and when you do, you make a different set of decisions that usually leaves Python as still the best language to use.
2
u/khne522 Jul 16 '18
Perhaps this more explicit writeup might be more useful to some people here. I've seen a similar one for C# and other JIT languages.
Also, tossing the 84x performance improvement from PyPy there is kind of irrelevant. Entirely application specific.
1
25
u/13steinj Jul 16 '18
Yet another person doesn't know the difference between strong typing and static typing, apparently.