Why Python Is Slow: Looking Under the Hood
jakevdp.github.io
Why Python Is Slow: Looking Under the Hood
1–10 of 156 posts
Re: Why Python Is Slow: Looking Under the Hood
#21. everything's a hash (object data, variable lookup, etc). jmoiron wrote a great article on the topic http://jmoiron.net/blog/whats-going-on/
2. refcounting introduces overhead for every variable access
3. loops are making all kinds of function calls to __iter__, next(), and catching StopIteration, which makes it hard to have a tight loop.
4. There's the GIL, builtin locking isn't great.
5. lots of indirection (lack of value types)
Re: Why Python Is Slow: Looking Under the Hood
#3This is at least 6 months old.
Re: Why Python Is Slow: Looking Under the Hood
#4Previously: https://news.ycombinator.com/item?id=7721096 This is at least 6 months old.
Re: Why Python Is Slow: Looking Under the Hood
#5Re: Why Python Is Slow: Looking Under the Hood
#6Why, oh, why does CPython bother keeping refcounts for small integers? Sure, it lets you make pretty graphs with [sys.getrefcount(i) for i in range(1000)]... but that's an extra memory read and write on every instruction that uses an integer. I can only imagine that not only are these extra instructions, but they're extra instructions that kill pipelining, if the interpreter needed to do "a = 1; b = 1; c = 1" for ins…
Re: Why Python Is Slow: Looking Under the Hood
#7Why, oh, why does CPython bother keeping refcounts for small integers? Sure, it lets you make pretty graphs with [sys.getrefcount(i) for i in range(1000)]... but that's an extra memory read and write on every instruction that uses an integer. I can only imagine that not only are these extra instructions, but they're extra instructions that kill pipelining, if the interpreter needed to do "a = 1; b = 1; c = 1" for ins…
Consider a C extension which takes an object and appends it to a list. If small integers did not have a refcount then that extension would have to have special code, like "if object is not a small integer, then increment the reference count".
Re: Why Python Is Slow: Looking Under the Hood
#8cPython does alot of work for just about any piece of code. Just a small handful of things: 1. everything's a hash (object data, variable lookup, etc). jmoiron wrote a great article on the topic http://jmoiron.net/blog/whats-going-on/ 2. refcounting introduces overhead for every variable access 3. loops are making all kinds of function calls to __iter__, next(), and catching StopIteration, which makes it hard to have…
Re: Why Python Is Slow: Looking Under the Hood
#9Why, oh, why does CPython bother keeping refcounts for small integers? Sure, it lets you make pretty graphs with [sys.getrefcount(i) for i in range(1000)]... but that's an extra memory read and write on every instruction that uses an integer. I can only imagine that not only are these extra instructions, but they're extra instructions that kill pipelining, if the interpreter needed to do "a = 1; b = 1; c = 1" for ins…
This isn't so bad on memory either, since Python keeps a static cache of integers that are reused on every int constructor call. Also allowing for slack, I wouldn't be surprised if the machine word came for free.
Re: Why Python Is Slow: Looking Under the Hood
#10Can we stop saying things like this? Virtually all Python is compiled, as part of the interpretation process.
There is a valid point here, of course. The point is that the top Python compilers do not compile to native code. So say that.
Words have meanings. Use them correctly.