json_speed.py 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import json
  2. import time
  3. dict_test = {'widget0': {"type": "div", "attributes": {"class": "container", "style": {"width": 240, "height": 240, "margin": 0, "padding": 0, "border-radius": 0, "border-width": 0, "border-color": "red", "background-color": 37864}}, "nodes": [{"type": "text", "attributes": {"font-size": "30", "class": "result-text", "style": {"top": 5, "left": 5, "width": 220, "text-align": "right", "height": 30, "color": "white", "text-overflow": "longbreak", "border-width": 0, "border-color": "red", "background-color": "transparent"}}, "nodes": [], "bindings":[{"attrName": "text", "key": "result", "isText": True}], "events": [], "value":"0", "widgetName":"widget1"}, {"type": "div", "attributes": {"class": "key-wrapper", "onclick": "onclick", "style": {"width": 60, "height": 50, "border-width": 0, "border-color": "red", "background-color": "transparent", "top": 40, "left": 10}}, "nodes": [{"type": "text", "attributes": {"font-size": "30", "class": "key", "style": {"left": 0, "top": 0, "width": 50, "height": 40, "margin": 0, "padding": 0, "color": "white", "font-size": 30, "text-align": "center", "background-color": "transparent"}}, "nodes": [], "bindings":[], "events":[], "value":"1", "widgetName":"widget3"}], "bindings":[], "events":[{"onclick": "onclick"}], "widgetName": "widget2"}, {"type": "div", "attributes": {"class": "key-wrapper", "onclick": "onclick", "style": {"width": 60, "height": 50, "border-width": 0, "border-color": "red", "background-color": "transparent", "top": 40, "left": 60}}, "nodes": [{"type": "text", "attributes": {"font-size": "30", "class": "key", "style": {
  4. "left": 0, "top": 0, "width": 50, "height": 40, "margin": 0, "padding": 0, "color": "white", "font-size": 30, "text-align": "center", "background-color": "transparent"}}, "nodes": [], "bindings":[], "events":[], "value":"2", "widgetName":"widget5"}], "bindings":[], "events":[{"onclick": "onclick"}], "widgetName": "widget4"}, {"type": "div", "attributes": {"class": "key-wrapper", "onclick": "onclick", "style": {"width": 60, "height": 50, "border-width": 0, "border-color": "red", "background-color": "transparent", "top": 40, "left": 110}}, "nodes": [{"type": "text", "attributes": {"font-size": "30", "class": "key", "style": {"left": 0, "top": 0, "width": 50, "height": 40, "margin": 0, "padding": 0, "color": "white", "font-size": 30, "text-align": "center", "background-color": "transparent"}}, "nodes": [], "bindings":[], "events":[], "value":"3", "widgetName":"widget7"}], "bindings":[], "events":[{"onclick": "onclick"}], "widgetName": "widget6"}, {"type": "div", "attributes": {"class": "key-wrapper", "onclick": "onclick", "style": {"width": 60, "height": 50, "border-width": 0, "border-color": "red", "background-color": "transparent", "top": 40, "left": 160}}, "nodes": [{"type": "text", "attributes": {"font-size": "30", "class": "key", "style": {"left": 0, "top": 0, "width": 50, "height": 40, "margin": 0, "padding": 0, "color": "white", "font-size": 30, "text-align": "center", "background-color": "transparent"}}, "nodes": [], "bindings":[], "events":[], "value":"+", "widgetName":"widget9"}], "bindings":[], "events":[{"onclick": "onclick"}], "widgetName": "widget8"}], "bindings": [], "events": [], "widgetName": "widget0"}}
  5. json.CONFIG_USING_CJSON = True
  6. print('dict_test:', dict_test)
  7. json_test = json.dumps(dict_test)
  8. print('json_test:', json_test)
  9. json.CONFIG_USING_CJSON = True
  10. start = time.tick_ms()
  11. for i in range(10):
  12. res = json.loads(json_test)
  13. str_res_cjson = str(res)
  14. print('len(str_res_cjson):', len(str_res_cjson))
  15. end = time.tick_ms()
  16. time_cjson_loads = end - start
  17. print('loads: cjson:', time_cjson_loads, 'ms')
  18. json.CONFIG_USING_CJSON = False
  19. start = time.tick_ms()
  20. for i in range(10):
  21. res = json.loads(json_test)
  22. str_res_jsmn = str(res)
  23. print('len(str_res_jsmn):', len(str_res_jsmn))
  24. end = time.tick_ms()
  25. time_jsmn_loads = end - start
  26. print('loads: jsmn:', time_jsmn_loads, 'ms')
  27. # test for dumps
  28. # test for dumps
  29. json.CONFIG_USING_CJSON = True
  30. start = time.tick_ms()
  31. for i in range(10):
  32. res = json.dumps(dict_test)
  33. len_res_cjson = len(res)
  34. print('len(res_cjson):', len_res_cjson)
  35. end = time.tick_ms()
  36. time_cjson_dumps = end - start
  37. print('dumps: cjson:', time_cjson_dumps, 'ms')
  38. json.CONFIG_USING_CJSON = False
  39. start = time.tick_ms()
  40. for i in range(10):
  41. res = json.dumps(dict_test)
  42. len_res_jsmn = len(res)
  43. print('len(res_jsmn):', len_res_jsmn)
  44. end = time.tick_ms()
  45. time_jsmn_dumps = end - start
  46. print('dumps: jsmn:', time_jsmn_dumps, 'ms')
  47. print('==================================================')
  48. print('loads: jsmn is', (time_cjson_loads /
  49. time_jsmn_loads), 'times faster than cjson')
  50. print('dumps: jsmn is', (time_cjson_dumps /
  51. time_jsmn_dumps), 'times faster than cjson')