3 ms·
The reason it's slow is because python interprets it's input. It essentially introduces major indirection for instructions to be executed. So for '1 + 1', you
by TheHydroImpulse 10y ago
The reason it's slow is because python interprets it's input. It essentially introduces major indirection for instructions to be executed.
So for '1 + 1', you can't just output some fast x86 instruction and inline it. You'll usually need to jump to where the python vm will do 'a + b' in C. You're having to do a bunch of redundant work at a higher level to emulate the CPU (sorta).
That's why you have JIT compilers that will read '1 + 1' and compile the appropriate machine code, store that in an executable memory region and jump to it. First time might be slow, but after that it's pretty fast. Because you need to jump to the dynamically generated code, you usually compile whole functions at a time.
This topic is super complicated but also super interested but I tried to simplify it a bit here.