Created
January 13, 2011 06:15
-
-
Save shazow/777482 to your computer and use it in GitHub Desktop.
Generators versus iterators.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment