wrote a blog post about some python coding styles and came across something that seemed very strange to me, and I was wondering if anyone understood what was happening to him. Basically, I have two versions of the same function:
a = lambda x: (i for i in range(x))
def b(x):
for i in range(x):
yield i
And I want to compare the performance of the two just created. In my opinion, this should include a small amount of computation, and both methods should approach zero, however, when I actually ran timeit:
def timing(x, number=10):
implicit = timeit.timeit('a(%s)' % int(x), 'from __main__ import a', number=number)
explicit = timeit.timeit('b(%s)' % int(x), 'from __main__ import b', number=number)
return (implicit, explicit)
def plot_timings(*args, **kwargs):
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x_vector = np.linspace(*args, **kwargs)
timings = np.vectorize(timing)(x_vector)
ax.plot(x_vector, timings[0], 'b--')
ax.plot(x_vector, timings[1], 'r--')
ax.set_yscale('log')
plt.show()
plot_timings(1, 1000000, 20)
I get a HUGE difference between the two methods, as shown below:

Where ais in blue, and b- in red.
Why is the difference so huge? It looks like the explicit version of the loop also grows logarithmically, and the implicit version does nothing (as it should).
Any thoughts?