Last active
March 17, 2025 22:58
-
-
Save malfet/a611d93f271f4cf6e0a48ff8d2f6bca7 to your computer and use it in GitHub Desktop.
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
import dis | |
import timeit | |
def list_to_dict_1(l): | |
rc = {} | |
for idx, v in enumerate(l): | |
rc[v] = idx | |
return rc | |
def list_to_dict_2(l): | |
return {v: idx for idx, v in enumerate(l)} | |
if __name__ == "__main__": | |
x = [str(i) for i in range(10_000)] | |
for f in (list_to_dict_1, list_to_dict_2): | |
print(f.__name__, timeit.timeit(lambda:f(x), number=5000)) | |
dis.dis(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using CPython-3.10 produces following: