Skip to content

Instantly share code, notes, and snippets.

@pathunstrom
Created May 14, 2018 18:38
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 pathunstrom/070a606e6fdf49575a55166c748737f6 to your computer and use it in GitHub Desktop.
Save pathunstrom/070a606e6fdf49575a55166c748737f6 to your computer and use it in GitHub Desktop.
Testing various ways to make safe iteration on sets
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