Last active
March 25, 2019 14:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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