Guest User

speedtest for dicts,sets,lists

a guest
Mar 27th, 2020
166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # speedtest dicts vs lists
  2. import time
  3.  
  4. d = {i: '' for i in range(10000000)}
  5. s = {i for i in range(10000000)}
  6. l = ['' for i in range(10000000)]
  7.  
  8. elem = 999999
  9. #membership testing 1 - lookups: speed in Decreasing order: dicts, sets, lists
  10.  
  11. start = time.perf_counter()
  12. if elem in s:
  13.     print('True')
  14.  
  15. end = time.perf_counter()
  16. print('time1 sets:' + str(end-start))
  17.  
  18. start = time.perf_counter()
  19.  
  20. if elem in d:
  21.     print('True')
  22.  
  23. end = time.perf_counter()
  24. print('time1 dicts:' + str(end-start))
  25.  
  26. start = time.perf_counter()
  27.  
  28. if elem in l:
  29.     print('True')
  30.  
  31. end = time.perf_counter()
  32. print('time1 lists:' + str(end-start))
  33.  
  34. # -----------------------------------------
  35. # inserting elements 2 --- speed in Decreasing order: dicts, sets, lists
  36.  
  37. start = time.perf_counter()
  38. s.add(elem)
  39.  
  40. end = time.perf_counter()
  41. print('time2 sets:' + str(end-start))
  42.  
  43. start = time.perf_counter()
  44. d[elem] = ''
  45.  
  46. end = time.perf_counter()
  47. print('time2 dicts:' + str(end-start))
  48.  
  49. start = time.perf_counter()
  50. l.insert(elem,1)
  51.  
  52. end = time.perf_counter()
  53. print('time2 lists:' + str(end-start))
  54.  
  55.  
  56. # -----------------------------------------
  57. # traversing elements 3 --- speed in Decreasing order: lists, dicts, sets
  58.  
  59. start = time.perf_counter()
  60. for item in s:
  61.     pass
  62.  
  63. end = time.perf_counter()
  64. print('time3 sets:' + str(end-start))
  65.  
  66. start = time.perf_counter()
  67. for item in d:
  68.     pass
  69.  
  70. end = time.perf_counter()
  71. print('time3 dicts:' + str(end-start))
  72.  
  73. start = time.perf_counter()
  74. for item in l:
  75.     pass
  76.  
  77. end = time.perf_counter()
  78. print('time3 lists:' + str(end-start))
RAW Paste Data