I prefer using nested functions instead of methods or global functions in Python whenever possible. So I decided to test their performance because it seams that when you define a function in another function there will be an overhead for the definition of the inner function in each call of the outer function.
At best I was hoping for the global function to be just slightly faster, but surprisingly the nested function was faster. Does anyone have any idea why?
This is my code:
from time import clock
def a(n):
return n + 1
def b1(loopcount):
return sum([a(n) for n in range(loopcount)])
def b2(loopcount):
def a(n):
return n + 1
return sum([a(n) for n in range(loopcount)])
powers = [5, 6, 7]
b1times = []
b2times = []
print " ", "".join(["{:^10d}".format(n) for n in powers])
for i in range(5):
for power in powers:
t = clock()
b1(10**power)
b1times.append(clock() - t)
for power in powers:
t = clock()
b2(10**power)
b2times.append(clock() - t)
print "b1:", "".join(["{:^10.5f}".format(n) for n in b1times])
print "b2:", "".join(["{:^10.5f}".format(n) for n in b2times])
print ""
b1times = []
b2times = []
And this is the result on my computer:
5 6 7
b1: 0.08200 0.82773 8.47946
b2: 0.06914 0.79637 8.18571
b1: 0.07332 0.82139 8.68262
b2: 0.06547 0.82088 8.19606
b1: 0.07963 0.82625 9.65037
b2: 0.06617 0.82027 8.21412
b1: 0.07630 0.82112 8.49082
b2: 0.06541 0.80686 8.20532
b1: 0.12328 0.87034 8.42964
b2: 0.07059 0.79717 8.24620
UPDATE: using @Janne Karila’s comment
Now that I’m calling b1 and b2 more, b1 becomes faster. So as @Kos and @Pavel Anossov said in their answers a few factors affect the speed here and you can’t make a general statement.
Thanks everyone!
from time import *
def a1(n):
return n + 1
def b1(n):
return a1(n)
def b2(n):
def a2():
return n + 1
return a2()
powers = [4, 5, 6]
b1times = []
b2times = []
print " ", "".join(["{:^10d}".format(n) for n in powers])
for i in range(5):
for power in powers:
t = clock()
sum([b1(n) for n in range(10**power)])
b1times.append(clock() - t)
for power in powers:
t = clock()
sum([b2(n) for n in range(10**power)])
b2times.append(clock() - t)
print "b1:", "".join(["{:^10.5f}".format(n) for n in b1times])
print "b2:", "".join(["{:^10.5f}".format(n) for n in b2times])
print ""
b1times = []
b2times = []
There are several parts that have an effect here:
1) The time to define the function (create the function object),
2) The time to look up the function object by name,
3) The time to actually call the function.
Your global function example is faster with 1) (no need to redefine
ain each call tob1). However, it is slower with in 2) because global variable lookup is slower than local lookup.Why can’t we have both then?
I’ve extended your benchmark with a solution that uses the global function, but avoids the global lookup using a local variable. It seems to be the fastest of the three on my machine: