Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hashing complexity is a burden for one off operations
"""Avoid hashing in usecases where you can't reach economies of scale."""
import time
i = list(range(1_000_000))
# Five linear searches (worst case)
start = time.time()
999_999 in i
999_999 in i
999_999 in i
999_999 in i
999_999 in i
end = time.time()
print(end - start)
# One hashed search (average case + setup costs)
start = time.time()
999_999 in set(i)
end = time.time()
print(end - start)
# 0.05650925636291504 (five searches)
# 0.05449104309082031 (one search)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.