Skip to content

Instantly share code, notes, and snippets.

@juanarrivillaga
Created May 7, 2017 21:31
Show Gist options
  • Save juanarrivillaga/d9fc9eae354516418c83dd5fe62cb6b9 to your computer and use it in GitHub Desktop.
Save juanarrivillaga/d9fc9eae354516418c83dd5fe62cb6b9 to your computer and use it in GitHub Desktop.
from timeit import timeit
from collections import deque
import string
def for_gen():
for i in range(100000):
yield i
def while_gen():
i = 0
while i < 1000000:
yield i
i += 1
t = timeit("deque(for_gen(), maxlen=1)", "from __main__ import for_gen, deque", number=100)
print("for_gen time: {} seconds".format(t))
t = timeit("deque(while_gen(), maxlen=1)", "from __main__ import while_gen, deque", number=100)
print("while_gen time: {} seconds".format(t))
def static(n):
alphabet = string.ascii_lowercase
return alphabet[n%25]
def kw_static(n, alphabet=string.ascii_lowercase):
return alphabet[n%25]
t = timeit("for i in range(1000000): d.append(static(i))", "from __main__ import static, deque, string; d = deque(maxlen=1)", number=100)
print("static time: {} seconds".format(t))
t = timeit("for i in range(1000000): d.append(kw_static(i))", "from __main__ import kw_static, deque, string; d = deque(maxlen=1)", number=100)
print("kw_static time: {} seconds".format(t))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment