-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique_sum.py
67 lines (52 loc) · 1.25 KB
/
unique_sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import random
import time
def unique_sum_list(nums):
total, seen = 0, list()
for n in nums:
if n in seen:
continue
total += n
seen.append(n)
return total
def unique_sum_tuple(nums):
total, seen = 0, tuple()
for n in nums:
if n in seen:
continue
total += n
seen = (*seen, n)
return total
def unique_sum_dict(nums):
total, seen = 0, dict()
for n in nums:
if n in seen:
continue
total += n
seen[n] = None
return total
def unique_sum_set(nums):
total, seen = 0, set()
for n in nums:
if n in seen:
continue
total += n
seen.add(n)
return total, seen
# Faster using sum and set in one line code
def unique_sum_set_fast(nums):
set_values = sum(set(nums))
return set_values
def main():
n = 100_000
nums = [random.randint(0, 2 << 32) for _ in range(n)]
for unique_sum in [
unique_sum_tuple,
unique_sum_list,
unique_sum_dict,
unique_sum_set,
]:
start = time.perf_counter()
total = unique_sum(nums)
elapsed = time.perf_counter() - start
print(f'{unique_sum.__name__}: {elapsed * 1000:.2f}')
main()