Is LOAD_GLOBAL really that slow?

A

Adam Olsen

It seems a common opinion that global access is much slower than local
variable access. However, my benchmarks show a relatively small
difference:

../python -m timeit -r 10 -v -s 'x = [None] * 10000
def foo():
for i in x:
list; list; list; list; list; list; list; list; list; list' 'foo()'
10 loops -> 0.0989 secs100 loops -> 0.991 secs
raw times: 0.999 0.985 0.987 0.985 0.985 0.982 0.982 0.982 0.981 0.985
100 loops, best of 10: 9.81 msec per loop

../python -m timeit -r 10 -v -s 'x = [None] * 10000
def foo():
mylist = list
for i in x:
mylist; mylist; mylist; mylist; mylist; mylist; mylist; mylist;
mylist; mylist' 'foo()'
10 loops -> 0.0617 secs
100 loops -> 0.61 secs
raw times: 0.603 0.582 0.582 0.583 0.581 0.583 0.58 0.583 0.584 0.582
100 loops, best of 10: 5.8 msec per loop

So global access is about 70% slower than local variable access. To
put that in perspective, two local variable accesses will take longer
than a single global variable access.

This is a very extreme benchmark though. In practice, other overheads
will probably drop the difference to a few percent at most. Not that
important in my book.

So my question: does somebody have a globals benchmark that shows a
really significant slowdown vs local variables?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top