Skip to content

Instantly share code, notes, and snippets.

@mulander
Created November 1, 2013 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mulander/7269424 to your computer and use it in GitHub Desktop.
Save mulander/7269424 to your computer and use it in GitHub Desktop.
# mulander@craig-laptop:~/art/bench$ python test.py
# 1.89928817749
# 1.24240493774
# mulander@craig-laptop:~/art/bench$ python test.py
# 1.93834280968
# 1.26033616066
# mulander@craig-laptop:~/art/bench$
import timeit
def flatten_iter(entry):
output = []
for item in entry:
for el in item:
output.append(el)
return output
def flatten_lcomp(entry):
return [el for item in entry for el in item]
print timeit.timeit("flatten_iter([[1], [2,3,45], [1,3], [6], [8]])", setup="from __main__ import flatten_iter")
print timeit.timeit("flatten_lcomp([[1], [2,3,45], [1,3], [6], [8]])", setup="from __main__ import flatten_lcomp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment