Skip to content

Commit f7c494a

Browse files
⚡️ Speed up method AlexNet._classify by 359%
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.
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 changed

code_to_optimize/code_directories/simple_tracer_e2e/workload.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def _extract_features(self, x):
4242

4343
def _classify(self, features):
4444
total = sum(features)
45-
return [total % self.num_classes for _ in features]
45+
mod_val = total % self.num_classes
46+
return [mod_val] * len(features)
4647

4748

4849
class SimpleModel:

0 commit comments

Comments
 (0)