Skip to content

Instantly share code, notes, and snippets.

@shazow
Created January 13, 2011 06:15
Show Gist options
  • Select an option

  • Save shazow/777482 to your computer and use it in GitHub Desktop.

Select an option

Save shazow/777482 to your computer and use it in GitHub Desktop.
Generators versus iterators.
def a(n):
for i in xrange(n):
yield i
def b(n):
r = []
for i in xrange(n):
r.append(i)
return r
def numerate(fn, n):
for i in fn(n):
pass
# >>> %timeit numerate(a, 1000)
# 10000 loops, best of 3: 112 us per loop
#
# >>> %timeit numerate(b, 1000)
# 10000 loops, best of 3: 155 us per loop
#
# >>> %timeit numerate(a, 10000)
# 1000 loops, best of 3: 1.11 ms per loop
#
# >>> %timeit numerate(b, 10000)
# 1000 loops, best of 3: 1.54 ms per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment