Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import timeit
  4. import json
  5. import rapidjson
  6. import pickle
  7.  
  8.  
  9. def mixed_dict(z):
  10. for x in range(z):
  11. d['tests' + str(x)] = 'this is a test ' + str(x)
  12. d['testi' + str(x)] = int(x)
  13. d['testf' + str(x)] = float(x)
  14. d[str(x)] = True
  15.  
  16.  
  17. def arrays(z):
  18. d['data'] = []
  19. d['data2'] = []
  20. for x in range(z):
  21. d['data'].append(x)
  22. d['data2'].append(x)
  23.  
  24.  
  25. for gen in (mixed_dict, arrays):
  26.  
  27. d = {}
  28.  
  29. means_json = []
  30. means_rapidjson = []
  31. means_pickle = []
  32.  
  33. def test_json():
  34. json.dumps(d)
  35.  
  36. def test_rapidjson():
  37. rapidjson.dumps(d)
  38.  
  39. def test_pickle():
  40. pickle.dumps(d)
  41.  
  42. tests = [test_json, test_rapidjson, test_pickle]
  43.  
  44. n = 100
  45.  
  46. for t in (10, 100, 1000, 10000):
  47. d.clear()
  48. gen(t)
  49. means_json.append(1 / timeit.timeit(stmt=test_json, number=n) * t)
  50. means_rapidjson.append(1 /
  51. timeit.timeit(stmt=test_rapidjson, number=n) * t)
  52. means_pickle.append(1 / timeit.timeit(stmt=test_pickle, number=n) * t)
  53.  
  54. # data to plot
  55. n_groups = 4
  56.  
  57. # create plot
  58. fig, ax = plt.subplots()
  59. index = np.arange(n_groups)
  60. bar_width = 0.2
  61. opacity = 0.8
  62.  
  63. rects1 = plt.bar(index,
  64. means_json,
  65. bar_width,
  66. alpha=opacity,
  67. color='b',
  68. label='JSON')
  69.  
  70. rects2 = plt.bar(index + bar_width,
  71. means_rapidjson,
  72. bar_width,
  73. alpha=opacity,
  74. color='g',
  75. label='RapidJSON')
  76.  
  77. rects3 = plt.bar(index + bar_width * 2,
  78. means_pickle,
  79. bar_width,
  80. alpha=opacity,
  81. color='orange',
  82. label='Pickle')
  83.  
  84. plt.xlabel('Data size')
  85. plt.ylabel('Score (serializations/sec * data_size)')
  86. plt.title('Score by algo (higher is better), data: ' + gen.__name__)
  87. plt.xticks(index + bar_width, ('10', '100', '1000', '10000'))
  88. plt.legend()
  89.  
  90. plt.tight_layout()
  91. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement