Skip to content

Instantly share code, notes, and snippets.

@ykshatroff
Last active April 18, 2023 12:27
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 ykshatroff/ca79ed32601cdc7c107297a147cfe7d3 to your computer and use it in GitHub Desktop.
Save ykshatroff/ca79ed32601cdc7c107297a147cfe7d3 to your computer and use it in GitHub Desktop.
Python's set `in` vs `not in` benchmark
"""
Performance of set `in` vs `not in`
"""
import random
import time
import string
spec = [
"".join(random.sample(string.ascii_letters, 40) )
for _ in range(20100)
]
def benchmark_in(original_set: set[str]):
return [x for x in spec if x in original_set]
def benchmark_not_in(complement_set: set[str]):
return [x for x in spec if x not in complement_set]
if __name__ == '__main__':
for x in (100, 1000, 10000, 20000):
print(f'--- {x} ---')
tested_set = set(random.sample(spec, x))
t = random.choice(spec)
tested_set_not_in = set(spec).difference(tested_set)
start = time.process_time()
r1= benchmark_in(tested_set)
res = time.process_time() - start
print(f"set({len(tested_set)}) in: {res:.12f}")
start = time.process_time()
r2= benchmark_not_in(tested_set_not_in)
res = time.process_time() - start
print(f"set({len(tested_set_not_in)}) not in: {res:.12f}")
assert r1 == r2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment