Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import numpy as np
  4. import time
  5.  
  6. n_choices = 18
  7.  
  8.  
  9. def switch_fun(x):
  10. if x == 0:
  11. return 0
  12. elif x == 1:
  13. return 1
  14. elif x == 2:
  15. return 2
  16. elif x == 3:
  17. return 3
  18. elif x == 4:
  19. return 4
  20. elif x == 5:
  21. return 5
  22. elif x == 6:
  23. return 6
  24. elif x == 7:
  25. return 7
  26. elif x == 8:
  27. return 8
  28. elif x == 9:
  29. return 9
  30. elif x == 10:
  31. return 10
  32. elif x == 11:
  33. return 11
  34. elif x == 12:
  35. return 12
  36. elif x == 13:
  37. return 13
  38. elif x == 14:
  39. return 14
  40. elif x == 15:
  41. return 15
  42. elif x == 16:
  43. return 16
  44. elif x == 17:
  45. return 17
  46.  
  47.  
  48. _switch_dict = dict((ii, ii) for ii in range(n_choices))
  49.  
  50.  
  51. def dict_fun(x):
  52. return _switch_dict.get(x)
  53.  
  54.  
  55. n_repeat = 1000
  56. tags = np.arange(500) % n_choices
  57. times = np.zeros((2, n_repeat))
  58. for ii, fun in enumerate((switch_fun, dict_fun)):
  59. for jj in range(n_repeat):
  60. t0 = time.time()
  61. for tag in tags:
  62. fun(tag)
  63. times[ii, jj] = time.time() - t0
  64.  
  65. times = np.median(times, axis=1)
  66. print('%0.1fx speedup for dict access' % (times[0] / times[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement