Skip to content

Instantly share code, notes, and snippets.

@hyunjun
Last active August 29, 2015 14:05
Show Gist options
  • Save hyunjun/108701fa3436710c39fd to your computer and use it in GitHub Desktop.
Save hyunjun/108701fa3436710c39fd to your computer and use it in GitHub Desktop.
timeit
# https://docs.python.org/2/library/timeit.html
>>> def foo(n):
... res = []
... for i in range(n): res.append(n)
... return res
...
>>> def foo2(n):
... return [i for i in range(n)]
...
>>> foo(10) == foo2(10)
False
>>> def foo(n):
... res = []
... for i in range(n): res.append(i)
... return res
...
>>> def foo2(n):
... return [i for i in range(n)]
...
>>> foo(10) == foo2(10)
True
>>> import timeit
>>> timeit.timeit(stmt='foo(10000)', setup='from __main__ import foo', number=100)
0.11489105224609375
>>> timeit.timeit(stmt='foo2(10000)', setup='from __main__ import foo2', number=100)
0.04136300086975098
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment