Performance with global variables vs local

Locals should be faster

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.

Locals are faster

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 global

Interestingly, 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).

Better optimization methods

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:

  1. Run your program with profiling on.
  2. Look at the profile in KCacheGrind or a similar program to determine Which functions are taking the most time.
  3. In those functions:
    • Look for places where you can cache results of functions (so you don’t have to do as much work).
    • Look for algorithmic improvements like replacing recursive functions with closed-form functions, or replacing list searches with dictionaries.
    • Re-profile to make sure the function is still a problem.
    • Consider using multiprocessing.

Leave a Comment