This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients Aaron Hall, rhettinger, serhiy.storchaka, terry.reedy
Date 2017-10-18.04:09:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1508299748.57.0.213398074469.issue31753@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2017-10-18 04:09:08rhettingersetrecipients: + rhettinger, terry.reedy, serhiy.storchaka, Aaron Hall
2017-10-18 04:09:08rhettingersetmessageid: <1508299748.57.0.213398074469.issue31753@psf.upfronthosting.co.za>
2017-10-18 04:09:08rhettingerlinkissue31753 messages
2017-10-18 04:09:08rhettingercreate