3 ms·
Isn’t that expected behaviour, as you’re only looking up “a” once when you do it outside the loop, while doing it every time when inside the loop? Because any
by stingraycharles 4y ago
Isn’t that expected behaviour, as you’re only looking up “a” once when you do it outside the loop, while doing it every time when inside the loop?
Because any reference in the whole hierarchy could change during the looping (e.g. one could say “multiple.levels = {}” at some point), the interpreter really would need to check it every time unless it can somehow “prove” that these changes will never happen / haven’t happened.
Just keeping a reference to “a” is semantically very different, and I’d consider that a normal optimisation.
- 20after4 4y agoThe issue is just how slow python is, it takes a very long time to resolve those references. So it's expected that the multiple.levels.of.dereference would be slower but it's perhaps orders of magnitude slower where in another language it might be much less of a performance hit.
- stingraycharles 4y agoMaybe, but then the loop has nothing to do with it, and the problem is rather the slow lookups.