Commit f7c494a
authored
⚡️ Speed up method
Here is an optimized version of your code. The main bottleneck is the list comprehension, which recalculates `total % self.num_classes` for every element in `features`, even though this value never changes within a single call. By computing it once and multiplying it with `[1]*len(features)` (to create the repeated list quickly), we save significant computation time. Also, using `len(features)` instead of iterating over `features` is slightly faster for large lists.
Here's the rewritten code.
**Changes made:**
- Compute `total % self.num_classes` only once and store in `mod_val`.
- Replace the list comprehension with a single multiplication: `[mod_val] * len(features)`.
This avoids both redundant modulo operations and Python's slower list comprehension for repeating a single value. The result and output remain exactly the same. The function is now allocation and compute efficient.AlexNet._classify by 359%1 parent 62efaf7 commit f7c494a
File tree
1 file changed
+2
-1
lines changed- code_to_optimize/code_directories/simple_tracer_e2e
1 file changed
+2
-1
lines changedLines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
| 46 | + | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
| |||
0 commit comments