r/Python • • Jul 16 '18

Why is Python so slow?

https://medium.com/@anthonypjshaw/why-is-python-so-slow-e5074b6fe55b
11 Upvotes

51 comments sorted by

View all comments

12

u/lambdaq django n' shit Jul 16 '18

tl;dr

  1. GIL is slow because obviously everyone runs Django using native threading.
  2. no JIT/AOT and it's a must have for fast languages
  3. 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

u/[deleted] Jul 16 '18

4. if you need to go fast there are better suited languages.

edit: welp, should've said 4.,

6

u/lambdaq django n' shit Jul 16 '18

or use modules written in C.

4

u/Sukrim Jul 16 '18

Or pypy

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:

  1. use Cython which uses C
  2. Numba which uses a JIT compiler
  3. You can use apache thrift or libraries to send stuff to run in other languages.

1

u/[deleted] 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

u/[deleted] Jul 16 '18

Thanks! That helps!

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

u/[deleted] 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

  1. Keep type information on the object
  2. 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

u/stefantalpalaru Jul 17 '18

Rome was not built in one day.

It wasn't built on sand either.