Commit e3fc180
authored
⚡️ Speed up method
Certainly! Below is a faster version of your program. It mainly optimizes the `_classify` method by avoiding recomputation of the same expression in the list comprehension and leveraging local variables to reduce attribute lookup overhead (Python does this faster).
**Explanation:**
- Precompute `sum(features) % self.num_classes` once instead of computing `sum(features)` and `% self.num_classes` for each element in the list comprehension.
- Use list multiplication (`[total_mod] * features_len`) which is much faster than list comprehension for repeating the same value.AlexNet._classify by 391%1 parent a162f0d commit e3fc180
1 file changed
Lines changed: 4 additions & 2 deletions
Lines changed: 4 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
| |||
0 commit comments