Function closure performance Function closure performance python-3.x python-3.x

Function closure performance


I assumed that since a is fixed when the closure is performed, the interpreter would precompute everything that involves a, and so math.sqrt(a) would be repeated just once instead of 1000000 times.

That assumption is wrong, I don't know where it came from. A closure just captures variable bindings, in your case it captures the value of a, but that doesn't mean that any more magic is going on: The expression math.sqrt(a) is still evaluated every time f is called.

After all, it has to be computed every time because the interpreter doesn't know that sqrt is "pure" (the return value is only dependent on the argument and no side-effects are performed). Optimizations like the ones you expect are practical in functional languages (referential transparency and static typing help a lot here), but would be very hard to implement in Python, which is an imperative and dynamically typed language.

That said, if you want to precompute the value of math.sqrt(a), you need to do that explicitly:

def g(a):  s = math.sqrt(a)  def f(b):    return s * b  return f

Or using lambda:

def g(a):   s = math.sqrt(a)  return lambda b: s * b

Now that g really returns a function with 1 parameter, you have to call the result with only one argument.


The code is not evaluated statically; the code inside the function is still calculated each time. The function object contains all the byte code which expresses the code in the function; it doesn't evaluate any of it. You could improve matters by calculating the expensive value once:

def g(a):    root_a = math.sqrt(a)    def f(b):        return root_a * b    return fresult = []a = 100func = g(a)for b in range(1000000):    result.append(func(b))

Naturally, in this trivial example, you could improve performance much more:

a = 100root_a = math.sqrt(a)result = [root_a * b for b in range(1000000)]

But I presume you're working with a more complex example than that where that doesn't scale?


As usual, the timeit module is your friend. Try some things and see how it goes. If you don't care about writing ugly code, this might help a little as well:

def g(a):   def f(b,_local_func=math.sqrt):      return _local_func(a)*b

Apparently python takes a performance penalty whenever it tries to access a "global" variable/function. If you can make that access local, you can shave off a little time.