|
1 | 1 | from concurrent.futures import ThreadPoolExecutor |
2 | | -from time import sleep |
3 | 2 |
|
4 | 3 |
|
5 | 4 | def funcA(number): |
6 | | - number = number if number < 1000 else 1000 |
| 5 | + number = number if number < 100 else 100 |
7 | 6 | k = 0 |
8 | | - for i in range(number * 100): |
| 7 | + for i in range(number * 10): |
9 | 8 | k += i |
10 | | - # Simplify the for loop by using sum with a range object |
11 | 9 | j = sum(range(number)) |
12 | | - |
13 | | - # Use a generator expression directly in join for more efficiency |
14 | 10 | return " ".join(str(i) for i in range(number)) |
15 | 11 |
|
16 | 12 |
|
17 | 13 | def test_threadpool() -> None: |
18 | | - pool = ThreadPoolExecutor(max_workers=3) |
19 | | - args = list(range(10, 31, 10)) |
| 14 | + pool = ThreadPoolExecutor(max_workers=2) |
| 15 | + args = [5, 10, 15] |
20 | 16 | result = pool.map(funcA, args) |
21 | 17 |
|
22 | 18 | for r in result: |
23 | 19 | print(r) |
24 | 20 |
|
25 | 21 | class AlexNet: |
26 | | - def __init__(self, num_classes=1000): |
| 22 | + def __init__(self, num_classes=10): |
27 | 23 | self.num_classes = num_classes |
28 | | - self.features_size = 256 * 6 * 6 |
29 | 24 |
|
30 | 25 | def forward(self, x): |
31 | | - features = self._extract_features(x) |
32 | | - |
33 | | - output = self._classify(features) |
34 | | - return output |
35 | | - |
36 | | - def _extract_features(self, x): |
37 | | - result = [] |
38 | | - for i in range(len(x)): |
39 | | - pass |
40 | | - |
41 | | - return result |
42 | | - |
43 | | - def _classify(self, features): |
44 | | - total = sum(features) |
45 | | - return [total % self.num_classes for _ in features] |
46 | | - |
47 | | -class SimpleModel: |
48 | | - @staticmethod |
49 | | - def predict(data): |
50 | | - result = [] |
51 | | - sleep(0.1) # can be optimized away |
52 | | - for i in range(500): |
53 | | - for x in data: |
54 | | - computation = 0 |
55 | | - computation += x * i ** 2 |
56 | | - result.append(computation) |
57 | | - return result |
58 | | - |
59 | | - @classmethod |
60 | | - def create_default(cls): |
61 | | - return cls() |
| 26 | + result = 0 |
| 27 | + for val in x: |
| 28 | + result += val * val |
| 29 | + return result % self.num_classes |
62 | 30 |
|
63 | 31 |
|
64 | 32 | def test_models(): |
65 | 33 | model = AlexNet(num_classes=10) |
66 | 34 | input_data = [1, 2, 3, 4, 5] |
67 | 35 | result = model.forward(input_data) |
68 | 36 |
|
69 | | - model2 = SimpleModel.create_default() |
70 | | - prediction = model2.predict(input_data) |
71 | | - |
72 | 37 | if __name__ == "__main__": |
73 | 38 | test_threadpool() |
74 | 39 | test_models() |
0 commit comments