Skip to content

Commit 867e138

Browse files
⚡️ Speed up method AlexNet._classify by 371%
Here is an optimized version of your program. The main area to optimize is the `_classify` method: using `sum()` is already efficient, but `[val for _ in features]` can be replaced with list multiplication, which is faster in Python. We also avoid recalculating `total % self.num_classes` for every element. This runs faster, especially for large lists of `features`, because the modulo is computed just once and the resultant list is constructed in a single step.
1 parent 030e1e5 commit 867e138

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
@@ -36,8 +36,8 @@ def _extract_features(self, x):
3636
return result
3737

3838
def _classify(self, features):
39-
total = sum(features)
40-
return [total % self.num_classes for _ in features]
39+
total_mod = sum(features) % self.num_classes
40+
return [total_mod] * len(features)
4141

4242

4343
class SimpleModel:

0 commit comments

Comments
 (0)