Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. import timeit
  2. import matplotlib.pyplot as plt
  3.  
  4.  
  5. lengths = [2 ** i for i in xrange(15)]
  6.  
  7. list_time = []
  8. dict_time = []
  9. for l in lengths:
  10. list_time.append(timeit.timeit('%i in d' % (l/2), 'd=range(%i)' % l))
  11. dict_time.append(timeit.timeit('%i in d' % (l/2),
  12. 'd=dict.fromkeys(range(%i))' % l))
  13. print l, list_time[-1], dict_time[-1]
  14.  
  15. plt.figure(figsize=(8,4))
  16. plt.loglog(lengths, dict_time, label='dict')
  17. plt.loglog(lengths, list_time, label='list')
  18. plt.title('List vs Dict Lookup Time')
  19. plt.xlabel('Num of entries')
  20. plt.ylabel('Executing 10,000 times (s)')
  21. plt.legend()
  22. plt.savefig('list_vs_dict.pdf')
  23.  
  24. repeat_dict_time = []
  25. for l in lengths:
  26. repeat_dict_time.append(timeit.repeat('%i in d' % (l/2),
  27. 'd=dict.fromkeys(range(%i))' % l,
  28. repeat=10))
  29. plt.figure(figsize=(8,4))
  30. plt.loglog(lengths, [min(i) for i in repeat_dict_time], label='dict')
  31. plt.title('Dict Lookup Time')
  32. plt.xlabel('Num of entries in dict')
  33. plt.ylabel('Best of 10 executing 10,000 times (s)')
  34. plt.legend()
  35. plt.savefig('dict_lookup.pdf')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement