Advertisement
bwukki

Basic timing in py

Oct 9th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. #exercise comparing del on list vs dict in python 3
  2.  
  3. import time
  4.  
  5.  
  6. def test_list(in_list):
  7.     start = time.time()
  8.     for i in range(len(in_list)):
  9.         del in_list[0]
  10.     end = time.time()
  11.     return end-start
  12.  
  13.  
  14. def test_dict(in_dict):
  15.     start = time.time()
  16.     for i in range(len(in_dict)):
  17.         del in_dict[i]
  18.     end = time.time()
  19.     return end-start
  20.  
  21.  
  22. x = list(range(125000))
  23. y = {j: j for j in range(10000000)}
  24. test_size1 = len(x)
  25. test_size2 = len(y)
  26.  
  27. print("It took {} seconds to delete {} items from a list".format(test_list(x),test_size1))
  28. print("It took {} seconds to delete {} items from a dict".format(test_dict(y),test_size2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement