Skip to content

Instantly share code, notes, and snippets.

@antoinedelia
Last active March 15, 2022 22:05
Show Gist options
  • Save antoinedelia/7f52c8c7125783f3c792c89c49e85a1e to your computer and use it in GitHub Desktop.
Save antoinedelia/7f52c8c7125783f3c792c89c49e85a1e to your computer and use it in GitHub Desktop.
How to time multiple functions
from timeit import repeat
setup = 'x = ("a", "b", "c", "d"); idx = 2'
stmt_range = """
[x[i] for i in range(len(x)) if i != 2]
"""
stmt_slice = """
list(x[:2] + x[3:])
"""
stmt_pop = """
l = list(x)
l.pop(idx)
"""
stmt_del = """
l = list(x)
del l[idx]
"""
codes = [
(stmt_range, "range"), # 0
(stmt_slice, "slice"), # 1
(stmt_pop, "pop"), # 2
(stmt_del, "del"), # 3
]
for code in codes:
print(code[1], end=" ")
print(min(repeat(stmt=code[0], setup=setup, number=10**5)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment