5 ms·
So why is Lua so much faster than Python? I tried searching about this but primarily people discuss historical context. I'm curious about real language features
by Demiurge 13y ago
So why is Lua so much faster than Python? I tried searching about this but primarily people discuss historical context. I'm curious about real language features and whys.
- justincormack 13y agoThere is some discussion in this thread http://lambda-the-ultimate.org/node/3851 http://lambda-the-ultimate.org/node/3851 - Python is much more complex than Lua.
- jacques_chester 13y agoPartly because it is a simple language and that simplicity allows some optimisations that are hard in a more featuresome language. There's also the history of development. Lua has always been an embedded language and the Lua team have always had an eye cocked to size and speed. Finally, size. The Lua runtime is tiny, these days it fits into an L2 cache. If your program doesn't have a lot of data, the effects of cache locality may give you tremendous speedups.
- Demiurge 13y agoOk, so the difference seems 'huge' in size. But the language complexity difference doesn't seem proportionate, so I'm curious what the list of these additional python language features is.
- masklinn 13y ago> so I'm curious what the list of these additional python language features is. http://docs.python.org/2/reference/datamodel.html#special-method-names http://docs.python.org/2/reference/datamodel.html#special-me...
- RyanZAG 13y agoLua is a far smaller language than Python. Python's complexity makes it extremely difficult to optimize because feature A conflicts with feature B, while Lua you can make much more specific optimization code. Python's standard methods of creating new objects off common operations also leads to huge amounts of memory being used compared ('functional programming' to use the term of the moment) to Lua's more c-like mutate-in-place standard behavior.
- Demiurge 13y agoOk, these comments is exactly what I hear, but leaves me more curious. What exactly is this complexity? On the surface, Python doesn't seem much more complex. It's data-structures also seem more simple to me.
- Touche 13y agoif __name__ == "__main__"
- kazagistar 13y agoJust use the dir() function on the repl and start digging.
- driax 13y agoPython is fairly simple in implementation while being as flexible as possible. Look up the concept of descriptors for something fairly difficult to optimize when you consider __getattr__ or __getattribute__. Consider also that you can directly access the __dict__ of both an instance and of the class of that instance. You can monkey-patch the class extensively or directly into it's __dict__. You can even replace the __class__ reference on instance to make it a subtype of different class (which is insanely flexible). Then there's the stuff about metaclasses, which is fairly advanced and collides with all of the other flexibility. Basically pretty much anything not from __builtin__ or from a C extension is entirely mutable at any time. Consider one of the problems PyPy had with some packages. PyPy didn't support `locals()[42] = value`, which some packages actually depended upon. Nor does PyPy support `instance.__dict__[42] = value` which even more depended upon. Generally Python used a lot of simple concepts such as mutable dictionaries with heterogeneously-typed keys as the implementation of how classes, modules, etc works, which is flexible but very hard to optimize in any way (especially since they are mutable).
- Demiurge 13y agoI think understand the complex reality that dynamic features arising from 'everything is a nested dictionary', and I used monkey patching once to save my life, but that seems a lot like Luas use of metatables. Is Lua less mutable?
- snarfy 13y agoLua uses a register based virtual machine. Python is stack based. That is the primary difference.
- oscargrouch 13y agoMike Pall is behind Luajit.. Get him or Lars Bak to work in a jit compiler and you language will be fast as it can be.. Of course, bad designed language in terms of performance will suffer more to get into the state-of-the-art
- masklinn 13y agoMike Pall has noted design decisions of Python which make it much harder to optimize than Lua in the past: http://lua-users.org/lists/lua-l/2004-11/msg00083.html http://lua-users.org/lists/lua-l/2004-11/msg00083.html
- Demiurge 13y agoThank you, that's the kind of stuff I was looking for.
- fdr_cs 13y agoBut , even the standard lua interpreter is faster then python
- copx 13y agoHere is one major reason: I have read that Guido van Rossum rejects all CPython patches which simply improve performance, because he thinks Python is "fast enough" and is unwilling to make the code more complex / risk the introduction of new bugs just to make Python faster. In contrast, the Lua developers always cared a lot about efficiency, not just speed, also memory use, and even GC pause times. They simply have different priorities than the CPython developers.