-
Notifications
You must be signed in to change notification settings - Fork 107
Optimised imports - api.py #91
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
Conversation
arthurauffray
commented
Jun 12, 2024
- Fixed imports to only import required items instead of the entire module (faster execution, reduced resource usage)
- Fixed imports to only import required items instead of the entire module (faster execution, reduced resource usage)
The impact on memory is 0, the entire module is always imported, regardless if you use
The impact on speed is ~0, the only difference is an extra name lookup because of the dotted access. |
Hey Plutaniano, thanks for your comment; it got me to look into this more. I did some testing with some quick copilot-generated code: Specific import: from time import sleep
sleep(0.5) Module import: import time
time.sleep(0.5) Runner code: import time
def run():
total_time = 0
for _ in range(100):
start_time = time.time()
exec(open('./speed/1.py').read())
end_time = time.time()
execution_time = end_time - start_time
total_time += execution_time
average_time = total_time / 100
print(f"Specific import: {average_time} seconds")
total_time = 0
for _ in range(100):
start_time = time.time()
exec(open('./speed/2.py').read())
end_time = time.time()
execution_time = end_time - start_time
total_time += execution_time
average_time = total_time / 100
print(f"Module import: {average_time} seconds")
for _ in range(5):
run() Here are the results (each pair of results runs each script 100 times for a total of 1000 executions):
Average Specific import time: 0.50406675529 seconds As for the memory usage, it is a fair point as it does still import the entire module; I think this could be changed 🤣. |
Your methodology for measuring is not ideal. Time fluctuations in the execution of
The impact on legibility is not worth it.
|
The idea of optimizing a python program like this, regardless of whether or not it's in a web API wrapper, is inherently very silly. |
@kratzky There are also edits to imports and corrected method calls in the code. I ran the tests, it works correctly. Can you merge |