Skip to content

Instantly share code, notes, and snippets.

@Louis-Saglio
Last active March 25, 2019 14:39
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 Louis-Saglio/b6b346dc22cf9a7dc427ca46db1270f3 to your computer and use it in GitHub Desktop.
Save Louis-Saglio/b6b346dc22cf9a7dc427ca46db1270f3 to your computer and use it in GitHub Desktop.
determine if instanciating and checking presence in a set is more efficient than instanciating and checking in a list or a tuple
from random import randint
from time import time
# Runtime values
i, j, k = randint(0, 9), randint(0, 9), randint(0, 9)
start = time()
for _ in range(10_000_000):
1 in {i, j, k}
print("Set in with runtime values", round(time() - start, 2))
start = time()
for _ in range(10_000_000):
8 in {i, j, k}
print("Set not in with runtime values", round(time() - start, 2))
start = time()
for _ in range(10_000_000):
1 in [i, j, k]
print("List in with runtime values", round(time() - start, 2))
start = time()
for _ in range(10_000_000):
8 in [i, j, k]
print("List not in with runtime values", round(time() - start, 2))
start = time()
for _ in range(10_000_000):
1 in (i, j, k)
print("Tuple in with runtime values", round(time() - start, 2))
start = time()
for _ in range(10_000_000):
8 in (i, j, k)
print("Tuple not in with runtime values", round(time() - start, 2))
# Compile-time values
start = time()
for _ in range(10_000_000):
1 in {4, 5, 6}
print("Set in with compile-time values", round(time() - start, 2))
start = time()
for _ in range(10_000_000):
8 in {4, 5, 6}
print("Set not in with compile-time values", round(time() - start, 2))
start = time()
for _ in range(10_000_000):
1 in [4, 5, 6]
print("List in with compile-time values", round(time() - start, 2))
start = time()
for _ in range(10_000_000):
8 in [4, 5, 6]
print("List not in with compile-time values", round(time() - start, 2))
start = time()
for _ in range(10_000_000):
1 in (4, 5, 6)
print("Tuple in with compile-time values", round(time() - start, 2))
start = time()
for _ in range(10_000_000):
8 in (4, 5, 6)
print("Tuple not in with compile-time values", round(time() - start, 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment