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
Posted On: Wednesday 7th of November 2012 01:31:09 PM Total Views:  177
View Complete with Replies

RELATED TOPICS OF Python Programming PROGRAMMING LANGUAGE




I really should know how to do this...

I've tried ctrl-refresh. Nadda. From the source I tried visiting some of the other pages on the site. I can see them all. Just not the index. I'm using IE 6 on WinXP. I tried it in Firefox and it worked. You'll still have to make your site compatible with IE 6 (if that is the problem) since 90%+ () of the population still use it.
VIEWS ON THIS POST

128

Posted on:

Wednesday 17th October 2012
View Replies!

is there really no good gui builder

whoever I ask, tells me when it come to python and GUI-s and that there is the best way to use WX. I am browsing for the 10th time during the last year and I can still not bealive that there is not one project to make gui-building easy as maybe in VB for python. Each I tried was a pain in the ass when it comes to usability. The only descent one I've seen was Boa constructor, but also they have stoped in developing. Please tell me that there is at least something descent. I am freaking out that I need 5 times more time to make a GUI in python than in VB.
VIEWS ON THIS POST

217

Posted on:

Sunday 4th November 2012
View Replies!

when does the GIL really block?

I have followed the GIL debate in python for some time. I don't want to get into the regular debate about if it should be gotten rid of (though I am curious about the status of that for Python 3)... personally I think I can do multi-threaded programming well, but I also see the benefits of a multiprocess approach. I'm not so egotistical that I don't realize perhaps my mt programming has not been "right" (though it worked and was debuggable) or more likely that doing it right I have avoided even trying some things people want mt programming to do... i.e. to do mt programming right you start to use queues a lot, inter-thread asynchronous, non-blocking, communication, which is essentially the multi-process approach, using IPC (except that the threads can see the same memory when, in your special case, you know that's ok. Given something like a reader-writer lock, this can have benefits... but again, whatever. My question is that given this problem, years ago before I started writing in python I wrote some short programs in python which could, in fact, busy both my CPUs. In retrospect I assume I did not have code in my run function that causes a GIL lock... so I have done this again. I start two threads... I use gkrellm to watch my processors (dual processor machine). If I merely print a number... both CPUS are getting 90% simultaneous loads. If I increment a counter and print it too, the same, and if I create a small list and sort it, the same. I did not expect this... I expected to see one processor pegged at around 100%, which should sometimes switch to the other processor. Granted, the same program in C/C++ would peg both processors at 100%... but given that the overhead in the interpreter cannot explain the extra usage, I assume the code in my thread's run functions is actually executing non-serially. I assume this is because what I am doing does not require the GIL to be locked for a significant part of the time my code is running... what code could I put in my run function to see the behavior I expected What code could I put there to take advantage of the possibility that really the GIL is not locked enough to cause actual serialization of the threads... anyone care to explain
VIEWS ON THIS POST

95

Posted on:

Sunday 4th November 2012
View Replies!

Somebody *really* got fond of python

http://xkcd.com/413/
VIEWS ON THIS POST

116

Posted on:

Sunday 4th November 2012
View Replies!

need really help

respected please help me i am really need of money please pay me through donation from my site. http://www.computersolution.co.cc i will be very thankful to you . please donate atleast 5$ or 2$ through my site http://www.computersolution.co.cc hope i will be able to clear my debts because of you all
VIEWS ON THIS POST

151

Posted on:

Sunday 4th November 2012
View Replies!

really small values

In multiplying a value of xe^-325 with ye^-4, Python is returning zero. How do I get it to give me back my tiny value
VIEWS ON THIS POST

148

Posted on:

Monday 5th November 2012
View Replies!

PyQt: Is signal / slot really working across threads?

pyqt users, i tried to use signal / slot across threads. With the following example I want to emit a signal when the thread loop is entered. The connected slot is never called. Why Any help is very welcome ... Alexander import time import sys import PyQt4 from PyQt4.QtCore import (QObject, QThread) SIGNAL = PyQt4.QtCore.SIGNAL class CancelableQtThread_(QThread): def __init__(self): QThread.__init__(self) self.sigStarted = SIGNAL("sigStarted()") def run(self): print "Enter thread" self.emit(self.sigStarted) time.sleep(0.1) print "Leave thread" class TestSigSlot(QObject): def __init__(self): QObject.__init__(self) self._thread = CancelableQtThread_() self.connect(self._thread, self._thread.sigStarted, self.Called) self._thread.start() time.sleep(1.0) def Called(self): print "Called !" if __name__ == "__main__": obj = TestSigSlot()
VIEWS ON THIS POST

211

Posted on:

Monday 5th November 2012
View Replies!

Demand that Obama release his college records! Where is the media?

Why is it important to see Obama's college records Because the media has created fantasies around Obama on every issue. You should start dispelling them with the easiest one, i.e. the fantasy of Obama's "academic degrees and honors". Obama has been fighting desperately to block all access to his college records. Even his "academic paper" at Columbia mysteriously disappeared. The media just swallowed the official Obama's "academic degrees and honors" without checking his actual grades, his scores on standardized tests, his academic writings if any, etc. the decisions to admit Obama to colleges, to give him "academic degrees and honors" were made by a few unknown individuals. Read about the racial turmoil at one college where he got his "academic honors". The race tensions seem to follow Obama wherever he goes. If Obama's college records fail to meet the requirements for legitimate academic degrees and honors then Obama will be declared an academic fraud. Maybe then the media will start vetting Obama on other issues. Then we will be able to address the biggest fantasies like Obama's "ability to solve" the financial crisis which requires making executive decisions. Obama never made a single real executive decision in his life! How will Obama be able to attract quality people if he has a long history of attracting and being attracted to the worst kind Any Hollywood actor can read a teleprompter and memorize talking points written by secret handlers. If Obama gets elected and turns the United States into a war-torn country like Kenya then his college records will mysteriously leak, but then it could be too late. I am asking everybody to request at least one major news network to make it an election issue. Ask others to do the same. Since Obama has built his political career around his "academic degrees and honors", all his college records (complete set of grades, scores on standardized tests, etc) must be released. Keep in mind that Obama's supporters have saturated media outlets and internet bandwidth for the last two years with stories of Obama's "academic degrees and honors". Now they viciously attack those who want to see Obama's grades and scores on standardized tests. Obama's "academic degrees and honors" without checking his grades and his scores on standardized tests are like "good standing" of Enron or Fannie Mae before they collapsed. You would be surprised what racial accusations, threats, boycotts can do to academic integrity at US colleges! Some websites to submit your requests: http://my.barackobama.com/page/s/contact2 http://abcnews.go.com/Site/pageid=3068843 http://www.msnbc.msn.com/id/6872152/ http://www.cnn.com/feedback/ http://www.foxnews.com/story/0,2933,77538,00.html http://www.cbsnews.com/ Some websites to read about Obama's "academic career": http://atlasshrugs2000.typepad.com/a...-mythical.html http://www.investors.com/editorial/e...07579834298611 http://www.nysun.com/new-york/obamas...mystery/85015/ http://www.jpost.com/servlet/Satelli...cle%2FShowFull http://worldnetdaily.com/index.phpf...w&pageId=74877 http://reason.com/news/show/128461.html
VIEWS ON THIS POST

112

Posted on:

Wednesday 7th November 2012
View Replies!

parsing a site/page that uses/calls javascript functions...

Hi... I've got a couple of test apps that I use to parse/test different html webpages. However, I'm now looking at how to parse a given site/page that uses javascript calls to dynamically create/display the resulting HTML. I can see the HTML is the Browser page if I manually select the btn that invokes the javascript function, but I have no idea how to create an app that can effectively parse the page. My test apps use python, along with mechanize/browser/urllib. I've seen sites/docs that discuss selenium, spidermonkey, etc... If possible, I'm trying to find a complete example (that walks through how to setup the environment, to how to finally extract the DOM elements of a given javascript page), or I'm looking to find someone I can work with, to create a complete example that can then be posted to the 'net. I'd really rather have a headless browser solution, as my overall goal is to run a parsing/crawling over a number of pages that utilize javascript.. Pointers, thoughts, comments, etc will be greatly appreciated.
VIEWS ON THIS POST

170

Posted on:

Wednesday 7th November 2012
View Replies!

Places that are hiring in my area

.. .. .. Enough to seek places that are hiring! You've already found the best site! Visit link below! ************************************ http://med247.us/p=job+hiring ************************************ .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. I was thankful that places that are hiring was a little worse for wear. I was thankful that now hiring was a little worse for wear. I was thankful that jobs hiring in my area was a little worse for wear. I was thankful that fast hiring jobs was a little worse for wear. I was thankful that who's hiring right now was a little worse for wear. I was thankful that jobs for 14 year olds was a little worse for wear.
VIEWS ON THIS POST

158

Posted on:

Wednesday 7th November 2012
View Replies!

We have over 700 companies that will pay you

We have over 700 companies that will pay you We have over 700 companies that will pay you to: Take online surveys - Get paid from $5 to $125 Each Survey! Get paid to drive your car $1000 to $3000 per month! Participate in Focus Groups - Get Paid up to $150 per hour Try new products and paid $10 to $25 or even more (Keep the products FREE!) Take telephone surveys and get paid up to $120 per hour. Preview new movie trailers and get paid anywhere from $10 to $25 per hour. Get paid to take surveys offline and get paid $5 to $95 per hour Get Paid $10 to $40 per hour To Shop and Eat! Get Paid $10 to $50 To Set Appointments online! Get Paid To Travel - Earn $100's each month just for traveling! Join Now :http://easylnk.com/6901 in God we trust Good Luck
VIEWS ON THIS POST

74

Posted on:

Wednesday 7th November 2012
View Replies!

Returning the positions of a list that are non-zero

I have a very large list of integers representing data needed for a histogram that I'm going to plot using pylab. However, most of these values (85%-95%) are zero and I would like to remove them to reduce the amount of memory I'm using and save time when it comes to plotting the data. To do this, I'm trying to find the best way to remove all of the zero values and produce a list of indices of where the non-zero values used to be. For example, if my original list is [0,0,1,2,1,0,0] I would like to produce the lists [1,2,1] (the non zero values) and [2,3,4] (indices of where the non-zero values used to be). Removing non-zero values is very easy but determining the indicies is where I'm having difficulty.
VIEWS ON THIS POST

83

Posted on:

Wednesday 7th November 2012
View Replies!

Re: 'string'.strip(chars)-like function that removes from the middle?

Cdric Lucantis wrote: > I don't see any string method to do that >>> 'abcde'.translate(str.maketrans('','','bcd')) 'ae' I do not claim this to be better than all the other methods, but this pair can also translate while deleting, which others cannot. , Sion Arrowsmith wrote: > In article , > Peter Otten wrote: >>Terry Reedy wrote: >>> >>> 'abcde'.translate(str.maketrans('','','bcd')) >>> 'ae' >>You should mention that you are using Python 3.0 >>The 2.5 equivalent would be >> >>>>> u"abcde".translate(dict.fromkeys(map(ord, u"bcd"))) >>u'ae' > > Only if you're using Unicode: .... which is what you do if you are using the str type in 3.0. Peter
VIEWS ON THIS POST

254

Posted on:

Wednesday 7th November 2012
View Replies!

Distributing applications that use 3rd party modules

, I'm getting into Python now after years of Perl, and as part of my research I must understand how to do some common tasks I need. I have a bunch of Windows PCs at work to which I want to distribute an application I've developed on my PC. these PCs have Python 2.5 installed. If my application contains only code I've developed, I simply zip its directory with .py files and send it to , who can then use it by running the entry-point .py file. However, what if I've installed some 3rd party modules on my PC, and my application uses them (for example pyparsing, PiYAML and some others) I don't want to manually install all these packages (there may be dozens of them) on all those PCs (there may be dozens of those too). What is the best method I can use Naturally, I want all the non-standard packages my app uses to be detected automatically and collected into some kind of convenient distributable that is easy to pass around and run. I'm aware of py2exe - tried it and it works fine. But it creates huge executables, and I don't want to distribute those all the time. I much prefer a zipped directory of .py scripts that takes some 10s of KBs.
VIEWS ON THIS POST

114

Posted on:

Wednesday 7th November 2012
View Replies!

Python-based regular expression parser that allows patterns to callfunctions?

I am writing a filtering HTTP proxy (the site is http://xuproxy.sourceforge.net/). I want it to be compatible with Proxomitron (http://proxomitron.info/) filters. I need a regular expression parser that allows patterns to call functions (or more likely, class methods), to implement "matching commands" (look at the Proxmitron documentation to see what I mean). Does anyone know if such a library exists for Python, or do I have to write my own parser , pyparsing also includes a decorator function, traceParseAction, which will list out calls to parse actions, the tokens passed in, and the value returned or exception raised. If you add "@traceParseAction" before each of the parse actions in my example, you can see the token processing being done in the parse actions. -- Paul
VIEWS ON THIS POST

282

Posted on:

Wednesday 7th November 2012
View Replies!

How do I execute a command from within python and wait on thatcommand?

how do i exec a command (such as xcopy) from with win32 python and wait on that command to come to completion and also cleanly terminate the command shell
VIEWS ON THIS POST

133

Posted on:

Wednesday 7th November 2012
View Replies!

Processing XML that's embedded in HTML

I need to parse a fairly complex HTML page that has XML embedded in it. I've done parsing before with the xml.dom.minidom module on just plain XML, but I cannot get it to work with this HTML page. The XML looks like this: Owner 1 07/16/2007 No Doe, John 1905 S 3rd Ave , Hicksville IA 99999 Owner 2 07/16/2007 No Doe, Jane 1905 S 3rd Ave , Hicksville IA 99999 It appears to be enclosed with The rest of the document is html, javascript div tags, etc. I need the information only from the row where the Relationship tag = Owner and the Priority tag = 1. The rest I can ignore. When I tried parsing it with minidom, I get an ExpatError: mismatched tag: line 1, column 357 so I think the HTML is probably malformed. I looked at BeautifulSoup, but it seems to separate its HTML processing from its XML processing. Can someone give me some pointers I am currently using Python 2.5 on Windows XP. I will be using Internet Explorer 6 since the document will not display correctly in Firefox. Thank you very much! Mike
VIEWS ON THIS POST

84

Posted on:

Wednesday 7th November 2012
View Replies!

Building libraries that my extensions can use. [distutils]

all, I want to create a shared object that my extension modules can dynamically load with intact symbols across modules. Searching the documentation lead me to distutils.ccompiler. Quite frankly comprehending this has been difficult for the newbie in me. I did google (..and krugle) for setup scripts that use this but they seem to be designed for medium to large projects. Mine is quite small to modify the ccompiler class. o (foo.so) / \ / \ / \ / \ bar.c baz.c So in my setup script i would like to have 'foo' under the libraries list in setup(). Is there a dummy' guide for this, or atleast a pretty basic example which I might pick upon given the simplicity. I did try this libraries=[("foo", {'sources'=src_dir, 'include_dirs'=dir, 'libraries' = some_external_thingy})] And it seems to build fine but I find that symbols in the object file are not visible to the others.. Am I in a state of confusion or what...=(, Anyone please guide me out of this by pointing me to a trivial example..
VIEWS ON THIS POST

72

Posted on:

Wednesday 7th November 2012
View Replies!

CGI Server that supports redirects

CGIHTTPServer does not support redirects[1] Is there an other python-only way to get a web server running wich can execute python code Since I already use flup[2]. I think there is not much missing to get it serving as http server. Has anyone hints [1] http://docs.python.org/lib/module-CGIHTTPServer.html [2]http://trac.saddi.com/flup -- Thomas Gttler, http://www.thomas-guettler.de/ http://www.tbz-pariv.de/ E-Mail: guettli (*) thomas-guettler + de Spam Catcher: niemand.leermann@thomas-guettler.de
VIEWS ON THIS POST

129

Posted on:

Wednesday 7th November 2012
View Replies!

creating really big lists

Hi! I would like to create a pretty big list of lists; a list 3,000,000 long, each entry containing 5 empty lists. My application will append data each of the 5 sublists, so they will be of varying lengths (so no arrays!). Does anyone know the most efficient way to do this I have tried: list = [[[],[],[],[],[]] for _ in xrange(3000000)] but its not soooo fast. Is there a way to do this without looping David.
VIEWS ON THIS POST

300

Posted on:

Wednesday 7th November 2012
View Replies!