Skip to content

Instantly share code, notes, and snippets.

@microamp
Created June 5, 2014 00:35
Show Gist options
  • Save microamp/d194ced49d3cf37b3810 to your computer and use it in GitHub Desktop.
Save microamp/d194ced49d3cf37b3810 to your computer and use it in GitHub Desktop.
Python: Lazy vs Strict
In [1]: from operator import add

In [2]: reduce(add, map(add, range(1000000), range(1000000)))
Out[2]: 999999000000

In [3]: %timeit reduce(add, map(add, range(1000000), range(1000000)))
10 loops, best of 3: 153 ms per loop

In [4]: %timeit reduce(add, map(add, xrange(1000000), xrange(1000000)))
10 loops, best of 3: 111 ms per loop

In [5]: from itertools import imap

In [6]: %timeit reduce(add, imap(add, xrange(1000000), xrange(1000000)))
10 loops, best of 3: 92.5 ms per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment