473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 2031
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
2
by: John Beschler | last post by:
Why are you running this in global.asa anyway? There has to be at least 3 or 4 other BETTER ways to do this. The easiest way would be to create a VB script file that accomplishes the same thing...
1
by: Dan | last post by:
I am new to tuning and I am having some trouble with my Oracle 9.2.0.1. I have a database that I am trying to gain consistent performance out of but I seem to have changing execution plans. Due...
5
by: Roger Withnell | last post by:
I have tried using setTimeout to delay the execution of a function with: setTimeout('function()', ms) This works, but if the function has parameters: setTimeout('function(p1, p2, p3)', ms) ...
5
by: MLH | last post by:
Having identified a A97 bug in which acDialog causes undesirable side effects processing the opened form, I would like to replace the following code running in MyForm ... DoCmd.OpenForm...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
17
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also...
11
by: Joseph S. | last post by:
Hi all, how do I avoid typing the keyword "$this->" every time I need to reference a member of a class inside the class? (coming from a world of cozy auto-complete enabled Java / .Net IDEs I...
6
by: trungthanh78 | last post by:
Hello everyone, I'm totally new to the group and I would like to learn from you. Thank you in advance! I need to write a program to track whether a mathematical function has changed during...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.