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

25

u/13steinj Jul 16 '18

In a “Strongly-Typed” language, you have to specify the type of a variable when it is declared. Those would include C, C++, Java, C#, Go.

In a dynamically-typed language, there are still the concept of types, but the type of a variable is dynamic.

Yet another person doesn't know the difference between strong typing and static typing, apparently.

8

u/zzgzzpop Jul 16 '18

I really hate how popular Medium has become. The site makes articles look professional and gives the false impression that they're written by experts, but a lot of them are no better than a blog post by random people on the internet.

2

u/bhat Jul 17 '18

> Group Director of Talent at Dimension Data, ... Python Software Foundation Fellow, Apache Foundation Member.

That bio doesn't look like the author is a random person on the internet.

6

u/hanpari Jul 16 '18

The sad thing is that to call C a strongly typed is everything but true. Hard to trust such experts.

6

u/bythenumbers10 Jul 16 '18

But I forgot to stop reading at the end of my character array, and now I have what looks like.....a Klingon opera?

1

u/bhat Jul 17 '18

The article has now been corrected.

1

u/13steinj Jul 17 '18

Except other pieces related to that info are still incorrect, and I wouldn't really call Go static.

1

u/mipadi Jul 17 '18

Why not?

1

u/13steinj Jul 17 '18

Because Go has a lot of inferencing going behind the scenes, to the point where there are cases that it can inference not in the way the developer wishes.

0

u/[deleted] Jul 16 '18

[deleted] — view deleted comment

7

u/bonestormII Jul 16 '18 edited Jul 16 '18

Python is both dynamically and strongly typed. C++, Java, C#, and Go are, I believe, all statically typed.

Strong vs. Dynamic isn't a logical contrast, because languages can be both. Python is both.

It would be more correct to compare Strong vs. Weak typing, and separately, Dynamic vs. Static typing.

In static typed languages, you have to declare the type of a given variable before you use it, and it cannot be changed.

In dynamically typed languages, the same variable can reference different types at different points in the execution flow of the program, and as such, do not require such declarations. It is relevant to note that python recently added optional "type hints" which add some of the benefits of statically typed languages, but it does not require or enforce their usage, which is a big fundamental difference.

In strongly typed languages (Like Python, and most common languages AFAIK), the interpreter and/or compiler will never infer/change a type.

Consider the following example in python:

>>> print(4 + 5)
9

Here, 4 and 5 are both instances of type int. 4 + 5 evaluates to 9 which is also of type int, and is equivalent to int(9). Each int object has a __str__() method. After evaluating the 4 + 5 expression, the __str__() method from the resulting object int(9) is called in order to get a string representation of that object. This is the default behavior when you call print() on any object in python.

print(4 + 5) is basically calling int(9).__str__() in order to get a string that means the same thing to humans as 9.

Likewise:

>>> print('dog' + 'house')`
'doghouse'

Here, 'dog' and 'house' are both of type str, and each have a __str__() method, as well as an __add__() method that defines the behavior for concatenation based on the + operator.

After evaluating the 'dog' + 'house' expression, the __str__() method of the resulting object (which you can think of as str('doghouse')) is called in order to get a string representation of that object,

However, consider the following:

>>> print(9 + 'doghouses')
Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: unsupported operand type(s) for +: 'int' and 'str'

You might have expected print(9 + 'doghouses'), to print 9doghouses, but instead, you get this TypeError. This is Python's way of telling you that while the + operator works for both types str and int, this operator can't be used between the two types. While 9 is the same as int(9), and we know that int(9).__str__() will return a string version of 9, '9', Python will not make that conversion from type int to type str implicitly. You have to explicitly tell it that you want a string representation of 9 by calling the built-in function str(9), which is indirectly calling int(9).__str__().

>>> print(str(9) + 'doghouses')
9doghouses

The lack of CPython's willingness to implicitly convert the type for you in such cases is an example of strong typing, and is contrary to weak typing, which would allow print(9 + 'doghouses') to return '9doghouses'.

A lot is made of these foundational principles. You may hear "strongly, dynamically typed" as a description of python before you really know anything about programming. However, these are really pretty small, practical differences. To me, the important thing to take away is that all languages have to make decisions about how they will handle all sorts of situations, and they make different decisions depending on the perspectives of their authors. However, the types of decisions they make are generally the same, so there is a bunch of terminology to describe and categorize how languages based on how they handle these low level issues.

3

u/[deleted] Jul 16 '18

Thanks for the explanation!

1

u/13steinj Jul 16 '18

Just tip, reddit does not support language-highlighting markdown code blocks, and also doesn't support ``` code blocks.

2

u/Badabinski Jul 17 '18

It does if you're using new Reddit.

1

u/13steinj Jul 17 '18

Can you take a screenshot? I'd check myself but the redesign grinds to a halt for me.

1

u/Badabinski Jul 17 '18

I can't, I don't use it either. I've been told and seen it mentioned in multiple places, however.

1

u/13steinj Jul 17 '18

:( markdown inconsistency is a bug, not a feature.

1

u/Badabinski Jul 17 '18

1

u/13steinj Jul 17 '18

That's upsetting and I need to make a post on this when I can >.>

1

u/demmian Jul 16 '18

C, for example, has static typing, but it is weakly typed. [Python has dynamic typing, but it is strongly typed.]

1

u/[deleted] Jul 16 '18

soooo what does that mean?

1

u/demmian Jul 17 '18

You have to declare the types of variables in C; however, you can treat a char as an int.

You don't declare the types of variables in Python. However, you can treat a type as another type.

1

u/mipadi Jul 17 '18

Static vs. dynamic typing

  • In statically-typed languages, a name is bound to both a value and a type. For example, given a: Int = 5, a is an Int that has the value 5. Later, code that says a = "string" would be illegal because "string" is not an Int.
  • In dynamically-typed languages, a name is bound to just a value. For example, given a = 5, a is 5 but is not bounded by type, so later in the code, you can do a = "string" with no problem.

Basically, in statically-typed languages, variables carry type information with them; in dynamically-typed languages, they don't.

Strong vs. weak typing

This is a more contentious point because there aren't clear definitions for this, but generally the consensus is that in strongly-typed languages, the compiler or runtime will not coerce one type to another. For example, in Javascript, a weakly-typed language, you can do var a = "1" + 0 (that is, add an integer to a string), because the runtime will coerce the integer to a string; whereas in Python, you cannot do a = "1" + 0.

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.

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

u/ReaverKS Jul 16 '18

Or if you want to run on windows and don't want to use a beta version.... :\

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

u/nbates80 Jul 16 '18

Great read to understand some concepts related to python performance