Skip to content

⚡️ Speed up function funcA by 3,983% #436

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
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
23 changes: 13 additions & 10 deletions code_to_optimize/code_directories/simple_tracer_e2e/workload.py
Original file line number Diff line number Diff line change
@@ -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:
@@ -21,14 +20,15 @@ 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

@@ -43,15 +43,17 @@ def _classify(self, features):
total = sum(features)
return [total % self.num_classes for _ in features]


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]
@@ -60,6 +62,7 @@ def test_models():
model2 = SimpleModel.create_default()
prediction = model2.predict(input_data)


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