470,614 Members | 1,500 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

execution time with global variables

Hi Pythoners,

i have been using a lot of global variables in the python script i am working on. the global variables are shared and used by all various 'definitions' :

def name ():
global all globs...
.....

my question is: 'is it safe using global variables in term of its execution time?

i cant think of any other ways not to use the globs because some variables simply have to be shared among those definitions..

many thanks..
sarmin

---------------------------------
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
Jul 18 '05 #1
3 1908
sarmin kho <sa********@yahoo.com> wrote in
news:ma**************************************@pyth on.org:
i have been using a lot of global variables in the python script i am
working on. the global variables are shared and used by all various
'definitions' :

def name ():
global all globs...
.....

my question is: 'is it safe using global variables in term of its
execution time?
Is the program running fast enough? If so, the answer is 'yes'. If not then
using local variables might provide some speedup, but usually changing your
algorithm is a better way to optimise.

i cant think of any other ways not to use the globs because some
variables simply have to be shared among those definitions..

The usual solution is to define a class and then all your globals simply
become attributes within the class. Try doing it that way and see if you
like it.
Jul 18 '05 #2
sarmin kho wrote:
Hi Pythoners,

i have been using a lot of global variables in the python script i am
working on. the global variables are shared and used by all various
'definitions' :

def name ():
global all globs...
.....

my question is: 'is it safe using global variables in term of its
execution time?
Here is may attempt to measure it:

<time_global.py>
def f():
x = 1
x
x
x
x
x
x
x
x
x
x
def g():
global x
x = 1
x
x
x
x
x
x
x
x
x
x
</time_global.py>

$ timeit.py -s"from time_global import f" "f()"
1000000 loops, best of 3: 0.972 usec per loop
$ timeit.py -s"from time_global import g" "g()"
1000000 loops, best of 3: 1.13 usec per loop

If the methodology isn't flawed, the effect is hardly noticable, as a real
function will have other operations that take much longer than a variable
lookup.
i cant think of any other ways not to use the globs because some variables
simply have to be shared among those definitions..


For a one-off script I see no problem, but otherwise I'd suggest you think
again and stay away from global variables as much as possible; the cost in
maintenance time may by far eclipse the extra execution time.
As a dumb "better than nothing" recipe I suggest you bundle the globals in a
Bunch-like class and pass that explicitly to your functions, or convert the
functions into methods and keep the "global" state as instance attributes.

Peter
Jul 18 '05 #3
Peter Otten wrote:
As a dumb "better than nothing" recipe I suggest you bundle the globals in
a Bunch-like class and pass that explicitly to your functions, or convert
the functions into methods and keep the "global" state as instance
attributes.


Of course this is even slower:

<time_global.py continued>
class C:
pass

def h(c):
c.x = 1
c.x
c.x
c.x
c.x
c.x
c.x
c.x
c.x
c.x
c.x
</time_global.py>

$ timeit.py -s"from time_global import h, C" -s"c=C()" "h(c)"
1000000 loops, best of 3: 1.94 usec per loop

Peter

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by John Beschler | last post: by
1 post views Thread by Dan | last post: by
5 posts views Thread by Roger Withnell | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.