Skip to content

Commit f51a515

Browse files
⚡️ Speed up method AlexNet._classify by 333%
Here is an optimized version of your program. The primary optimization opportunity is replacing the use of `sum(features)` and the list comprehension `[total % self.num_classes for _ in features]` with a more efficient approach. Since the total and num_classes never change inside the method, we compute the modulo only once, then reuse it for all outputs with multiplication rather than generating a new list with a comprehension. **Explanation of optimizations:** - **sum(features) % self.num_classes** is computed once instead of computing `total % self.num_classes` for every element of `features`. - **List multiplication** (`[total_mod] * len(features)`) is faster than a list comprehension over the same value, as it doesn't loop nor call the modulo operator repeatedly.
1 parent fd9eb86 commit f51a515

File tree

1 file changed

+2
-2
lines changed
  • code_to_optimize/code_directories/simple_tracer_e2e

1 file changed

+2
-2
lines changed

code_to_optimize/code_directories/simple_tracer_e2e/workload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def _extract_features(self, x):
3838
return result
3939

4040
def _classify(self, features):
41-
total = sum(features)
42-
return [total % self.num_classes for _ in features]
41+
total_mod = sum(features) % self.num_classes
42+
return [total_mod] * len(features) # Return same value for all as total_mod
4343

4444

4545
class SimpleModel:

0 commit comments

Comments
 (0)