Raymond Hettinger <raymond.hettin...@gmail.com> added the comment: I question those timings. Here's the results from a script I've been using for many years:
$ python3.6 variable_access.py 0.065 read_local 0.068 read_nonlocal 0.179 read_global 0.236 read_builtin 0.267 read_classvar 0.392 read_instancevar 0.291 read_unboundmethod 0.383 read_boundmethod 0.077 write_local 0.069 write_nonlocal 0.240 write_global 1.154 write_classvar 0.540 write_instance See the attached timing script: variable_access.py Also, take a look at the underlying code: #define GETLOCAL(i) (fastlocals[i]) TARGET(LOAD_FAST) { PyObject *value = GETLOCAL(oparg); if (value == NULL) { ... } Py_INCREF(value); PUSH(value); FAST_DISPATCH(); } #define PyCell_GET(op) (((PyCellObject *)(op))->ob_ref) TARGET(LOAD_DEREF) { PyObject *cell = freevars[oparg]; PyObject *value = PyCell_GET(cell); if (value == NULL) { ... } Py_INCREF(value); PUSH(value); DISPATCH(); } You can see that the only difference is that LOAD_DEREF has one extra indirection. That should be very cheap. In contrast, a LOAD_GLOBAL does a lot more work. If this isn't evident in your timings, I suspect there is something wrong with the timings. ---------- Added file: https://bugs.python.org/file47225/variable_access.py _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue31753> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com