Created
January 9, 2020 05:47
-
-
Save srafay/8d8f19fa0c8e1a9400baabf0ec1892ab to your computer and use it in GitHub Desktop.
Profiling two functions using timeit in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from datetime import datetime | |
if __name__ == '__main__': | |
def test_get(): | |
x = {'x':'123'} | |
return x.get('x') | |
def test_dict(): | |
y = {'y':'123'} | |
return y['y'] | |
print(datetime.now()) | |
resp_x = timeit.repeat("test_get()", "from __main__ import test_get", number=100000) | |
print(datetime.now()) | |
resp_y = timeit.repeat("test_dict()", "from __main__ import test_dict", number=100000) | |
print(datetime.now()) | |
print("Min Get: {0}".format(min(resp_x))) | |
print("Min Dict: {0}".format(min(resp_y))) | |
print("Dict/Get: {0:.2f}".format((min(resp_x) / min(resp_y)))) | |
# for i in range(len(resp_x)): | |
# print("Dict/Get: {0:.2f}".format((resp_y[i]/resp_x[i])*100)) | |
# print(resp_x) | |
# print(resp_y) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment