Last active
April 18, 2023 12:27
-
-
Save ykshatroff/ca79ed32601cdc7c107297a147cfe7d3 to your computer and use it in GitHub Desktop.
Python's set `in` vs `not in` benchmark
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
""" | |
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