Skip to content

⚡️ Speed up method AlexNet.forward by 3,112% #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions code_to_optimize/code_directories/simple_tracer_e2e/workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@


def funcA(number):
number = number if number < 1000 else 1000
k = 0
for i in range(number * 100):
k += i
# Simplify the for loop by using sum with a range object
j = sum(range(number))
number = min(1000, number)
# Use arithmetic formula for sum instead of looping
k = (number * 100) * (number * 100 - 1) // 2
# Simplify the for loop by using sum with a range object (now by formula)
j = number * (number - 1) // 2

# Use a generator expression directly in join for more efficiency
return " ".join(str(i) for i in range(number))
# Use a map object for efficiency in join (str is faster than formatting and works well here)
return " ".join(map(str, range(number)))


def test_threadpool() -> None:
Expand All @@ -21,37 +20,37 @@ def test_threadpool() -> None:
for r in result:
print(r)


class AlexNet:
def __init__(self, num_classes=1000):
self.num_classes = num_classes
self.features_size = 256 * 6 * 6

def forward(self, x):
features = self._extract_features(x)

output = self._classify(features)
return output

def _extract_features(self, x):
result = []
for i in range(len(x)):
pass

return result
# Return an empty list directly; previous loop did nothing.
return []

def _classify(self, features):
total = sum(features)
return [total % self.num_classes for _ in features]
# Since features is always [], just return [] quickly.
return []


class SimpleModel:
@staticmethod
def predict(data):
return [x * 2 for x in data]

@classmethod
def create_default(cls):
return cls()


def test_models():
model = AlexNet(num_classes=10)
input_data = [1, 2, 3, 4, 5]
Expand All @@ -60,6 +59,7 @@ def test_models():
model2 = SimpleModel.create_default()
prediction = model2.predict(input_data)


if __name__ == "__main__":
test_threadpool()
test_models()
Loading