I am still new to Python, and I have been trying to improve the performance of my Python script, so I tested it with and without global variables. I timed it, and to my surp
According to this page on locals and globals:
When a line of code asks for the value of a variable x, Python will search for that variable in all the available namespaces, in order:
- local namespace - specific to the current function or class method. If the function defines a local variable x, or has an argument x, Python will use this and stop searching.
- global namespace - specific to the current module. If the module has defined a variable, function, or class called x, Python will use that and stop searching.
- built-in namespace - global to all modules. As a last resort, Python will assume that x is the name of built-in function or variable.
Based on that, I'd assume that local variables are generally faster. My guess is what you're seeing is something particular about your script.
Here's a trivial example using a local variable, which takes about 0.5 seconds on my machine (0.3 in Python 3):
def func():
for i in range(10000000):
x = 5
func()
And the global version, which takes about 0.7 (0.5 in Python 3):
def func():
global x
for i in range(1000000):
x = 5
func()
global does something weird to variables that are already globalInterestingly, this version runs in 0.8 seconds:
global x
x = 5
for i in range(10000000):
x = 5
While this runs in 0.9:
x = 5
for i in range(10000000):
x = 5
You'll notice that in both cases, x is a global variable (since there's no functions), and they're both slower than using locals. I have no clue why declaring global x helped in this case.
This weirdness doesn't occur in Python 3 (both versions take about 0.6 seconds).
If you want to optimize your program, the best thing you can do is profile it. This will tell you what's taking the most time, so you can focus on that. Your process should be something like: