473,671 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2043
sarmin kho <sa********@yah oo.com> wrote in
news:ma******** *************** *************** @python.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
2793
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 _build_used(): .... y = x + 1 .... return x, y ....
2
1835
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 (the code would be wuite similiar to what you have now) and schedule that VB file to run at 10:00 am every Friday morning using task scheduler. HTH,
1
2925
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 to a number of factors outside my control, I have to handle an application that does not send bind variables, I handle them as they come in. To do this, I have set the init.ora file to have an entry of 'cursor_sharing=similiar'. I know this is a...
5
11120
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) it does not work.
5
2894
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 "frmVehicleChooserForm", , , , , acDialog .... (more code I want to run after suspension) With this new approach... DoCmd.OpenForm "frmVehicleChooserForm", , , , , , ThisForm Me.Visible = False
17
5615
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 constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
17
5067
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 identifying memory leaks. The application in question is the Mozilla Web Browser. I also have had similar tasks before in the compiler construction area. And it is easy to come up with many more examples, where such kind of statistics can be very...
11
3074
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 find it a bit annoying) TIA, JS
6
1651
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 run-time or not. The program should work with any mathematical function provided by users. Let's take an example in the C language:
0
8472
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4221
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.