Created
May 14, 2018 18:38
-
-
Save pathunstrom/070a606e6fdf49575a55166c748737f6 to your computer and use it in GitHub Desktop.
Testing various ways to make safe iteration on sets
This file contains hidden or 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 copy import copy | |
from timeit import timeit | |
count = 1000000 | |
test_set = {str(x) for x in range(100)} | |
snippets = { | |
"list_comprehension": "list(list(test_set))", | |
"method intersection": "list(test_set.intersection(test_set))", | |
"copy": "list(copy(test_set))", | |
"binary and": "list(test_set & test_set)", | |
"frozenset": "list(frozenset(test_set))" | |
} | |
name_space = locals() | |
for name, snippet in snippets.items(): | |
result = timeit(snippet, number=count, globals=name_space) | |
print(f"Result of {name}: {result}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment