Skip to content

Commit 6b1bc53

Browse files
⚡️ Speed up method AlexNet._classify by 340%
Here is an optimized version of your AlexNet class. Your original code recalculates `total % self.num_classes` for every item in `features`, but this value does not change within the list comprehension. We can compute it once and repeat it, making the code faster and also a bit more memory efficient. **No comments were present to preserve.** This version calculates `sum(features) % self.num_classes` only once and multiplies into a list, which is faster than using a list comprehension that repeats the calculation.
1 parent d6667d5 commit 6b1bc53

File tree

1 file changed

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

1 file changed

+3
-2
lines changed

code_to_optimize/code_directories/simple_tracer_e2e/workload.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ 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+
# Compute total_mod once and repeat it for the length of features
43+
return [total_mod] * len(features)
4344

4445

4546
class SimpleModel:

0 commit comments

Comments
 (0)