Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.53 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()
  92.  divisor@mws3:/opt/workstuff git:(master) ✗ cat benchmark_serializers.py -p|tee
  93. import numpy as np
  94. import matplotlib.pyplot as plt
  95. import timeit
  96. import json
  97. import rapidjson
  98. import pickle
  99.  
  100.  
  101. def mixed_dict(z):
  102.     for x in range(z):
  103.         d['tests' + str(x)] = 'this is a test ' + str(x)
  104.         d['testi' + str(x)] = int(x)
  105.         d['testf' + str(x)] = float(x)
  106.         d[str(x)] = True
  107.  
  108.  
  109. def arrays(z):
  110.     d['data'] = []
  111.     d['data2'] = []
  112.     for x in range(z):
  113.         d['data'].append(x)
  114.         d['data2'].append(x)
  115.  
  116.  
  117. for gen in (mixed_dict, arrays):
  118.  
  119.     d = {}
  120.  
  121.     means_json = []
  122.     means_rapidjson = []
  123.     means_pickle = []
  124.  
  125.     def test_json():
  126.         json.dumps(d)
  127.  
  128.     def test_rapidjson():
  129.         rapidjson.dumps(d)
  130.  
  131.     def test_pickle():
  132.         pickle.dumps(d)
  133.  
  134.     tests = [test_json, test_rapidjson, test_pickle]
  135.  
  136.     n = 100
  137.  
  138.     for t in (10, 100, 1000, 10000):
  139.         d.clear()
  140.         gen(t)
  141.         means_json.append(1 / timeit.timeit(stmt=test_json, number=n) * t)
  142.         means_rapidjson.append(1 /
  143.                                timeit.timeit(stmt=test_rapidjson, number=n) * t)
  144.         means_pickle.append(1 / timeit.timeit(stmt=test_pickle, number=n) * t)
  145.  
  146.     # data to plot
  147.     n_groups = 4
  148.  
  149.     # create plot
  150.     fig, ax = plt.subplots()
  151.     index = np.arange(n_groups)
  152.     bar_width = 0.2
  153.     opacity = 0.8
  154.  
  155.     rects1 = plt.bar(index,
  156.                      means_json,
  157.                      bar_width,
  158.                      alpha=opacity,
  159.                      color='b',
  160.                      label='JSON')
  161.  
  162.     rects2 = plt.bar(index + bar_width,
  163.                      means_rapidjson,
  164.                      bar_width,
  165.                      alpha=opacity,
  166.                      color='g',
  167.                      label='RapidJSON')
  168.  
  169.     rects3 = plt.bar(index + bar_width * 2,
  170.                      means_pickle,
  171.                      bar_width,
  172.                      alpha=opacity,
  173.                      color='orange',
  174.                      label='Pickle')
  175.  
  176.     plt.xlabel('Data size')
  177.     plt.ylabel('Score (serializations/sec * data_size)')
  178.     plt.title('Score by algo (higher is better), data: ' + gen.__name__)
  179.     plt.xticks(index + bar_width, ('10', '100', '1000', '10000'))
  180.     plt.legend()
  181.  
  182.     plt.tight_layout()
  183.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement