-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
66 lines (56 loc) · 1.46 KB
/
test.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
from typing import List, Union
from time import time
from random import randint
from dataclasses import dataclass
class TestClass:
def __init__(self):
self.values = [randint(1, 5) for _ in range(0, 2)]
def __repr__(self):
return str(self.values)
_count_mapping = {
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five'
}
@dataclass
class Count:
one: int = 0
two: int = 0
three: int = 0
four: int = 0
five: int = 0
@classmethod
def from_map(cls, test_class: TestClass):
obj = cls()
for value in test_class.values:
obj.__dict__[
_count_mapping[value]
] += 1
return obj
@classmethod
def from_if_else(cls, test_class: TestClass):
obj = cls()
for value in test_class.values:
if value == 1:
obj.one += 1
elif value == 2:
obj.two += 1
elif value == 3:
obj.three += 1
elif value == 4:
obj.four += 1
elif value == 5:
obj.five += 1
return obj
test_classes = [TestClass() for _ in range(0, 100000)]
begin = time()
counts_by_map = [Count.from_map(test_class) for test_class in test_classes]
end = time()
print(f"By map took {end-begin}")
begin = time()
counts_by_if_else = [Count.from_if_else(test_class) for test_class in test_classes]
end = time()
print(f"By if else took {end-begin}")
print()