Skip to content

Instantly share code, notes, and snippets.

@srafay
Created January 9, 2020 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srafay/8d8f19fa0c8e1a9400baabf0ec1892ab to your computer and use it in GitHub Desktop.
Save srafay/8d8f19fa0c8e1a9400baabf0ec1892ab to your computer and use it in GitHub Desktop.
Profiling two functions using timeit in Python
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