Skip to content

Commit b035a35

Browse files
⚡️ Speed up method AlexNet.forward by 268%
Here's an optimized version of your program. Since `_extract_features` always returns an empty list, calling `_classify` with this empty list results in a sum of zero and `range(len(features))` is always empty, resulting in an empty list as output. This means the entire process can be shortcut: any value of `x` will result in a return value of `[]`, with no further computation. All the slow code is avoided. **Perf note:** The optimized `forward` function simply returns `[]` and does not instantiate intermediary lists or call redundant routines when it's clear from static analysis that the outputs are always empty. This is the fastest you can make this program without altering the class interface or logic.
1 parent 57edce8 commit b035a35

File tree

1 file changed

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

1 file changed

+3
-5
lines changed

code_to_optimize/code_directories/simple_tracer_e2e/workload.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ def __init__(self, num_classes=1000):
2323
self.features_size = 256 * 6 * 6
2424

2525
def forward(self, x):
26-
features = self._extract_features(x)
27-
28-
output = self._classify(features)
29-
return output
26+
# Directly return empty list, since _extract_features and _classify are effectively no-ops.
27+
return []
3028

3129
def _extract_features(self, x):
3230
# The original implementation does nothing and returns an empty list.
33-
# Optimized to directly return an empty list.
3431
return []
3532

3633
def _classify(self, features):
34+
# Retain original structure for compatibility; always returns empty list if features is empty.
3735
total = sum(features)
3836
return [total % self.num_classes for _ in features]
3937

0 commit comments

Comments
 (0)