471,336 Members | 1,272 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

Is LOAD_GLOBAL really that slow?

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?

--
Adam Olsen, aka Rhamphoryncus
Aug 30 '07 #1
0 801

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Zip Code | last post: by
5 posts views Thread by Rob R. Ainscough | last post: by
2 posts views Thread by Gary Wright | last post: by
1 post views Thread by z. f. | last post: by
7 posts views Thread by Fredrik Melin | last post: by
3 posts views Thread by michael | last post: by
reply views Thread by CptDondo | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.