3 ms·
This is why Python is so slow compared with Lua, which also does this with ints.
by frankreyes 3y ago
This is why Python is so slow compared with Lua, which also does this with ints.
- Asraelite 3y agoIn fairness, the developers of CPython don't prioritize making it fast very much. Pypy has comparable performance to LuaJIT though. Do you happen to know how Pypy represents ints internally?
- frankreyes 3y agoNo idea, but packing an int in a single 32/64 bit value without indirections is how V8 JS does it as well, hence the speed. I'm going to guess that pypy does it too.
- SushiHippie 3y agoAren't python integers basically represented as an array of 32bit integers? What does this have to do with 63 bit integers?
- frankreyes 3y agoPython integers are represented like any other data type: a pointer to a PyObject. In the specific case of small integers, they are recycled from a pool of known instances, so we save the malloc() call for those. But the penalty still exists for indirections and cache trashing and CPU predictions, and so on... It has to do that 63 bit integers, like Lua and V8 JS, they are much faster, because no malloc() or pointer indirection is required to do 1234+5678. It's just a few CPU instruction. In Python, it's like 100.
- SushiHippie 3y agoGot it, thanks!