Created
August 7, 2023 09:47
-
-
Save Veticus/5fe2bcb387d889c26c26d58878283037 to your computer and use it in GitHub Desktop.
speed diff in collection types is bonkers
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
def unique_sum_with_list(howMany): | |
sum = 0 | |
seen_numbers = [] | |
for n in range(howMany): | |
if n in seen_numbers: | |
continue | |
sum += n | |
seen_numbers.append(n) | |
print("list sum: " + str(sum)) | |
return sum | |
def unique_sum_with_set(howMany): | |
sum = 0 | |
seen_numbers = set() | |
for n in range(howMany): | |
if n in seen_numbers: | |
continue | |
sum += n | |
seen_numbers.add(n) | |
print("set sum: " + str(sum)) | |
return sum | |
def unique_sum_with_dict(howMany): | |
sum = 0 | |
seen_numbers = {} | |
for n in range(howMany): | |
if n in seen_numbers: | |
continue | |
sum += n | |
seen_numbers[n] = None | |
print("dict sum: " + str(sum)) | |
return sum | |
def unique_sum_with_tuple(howMany): | |
sum = 0 | |
seen_numbers = () | |
for n in range(howMany): | |
if n in seen_numbers: | |
continue | |
sum += n | |
seen_numbers = (*seen_numbers, n) | |
print("tuple sum: " + str(sum)) | |
return sum | |
if __name__ == '__main__': | |
how_many_numbers = 102400 | |
unique_sum_with_tuple(how_many_numbers) | |
unique_sum_with_dict(how_many_numbers) | |
unique_sum_with_set(how_many_numbers) | |
unique_sum_with_list(how_many_numbers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment