纯字符串assemble和parser比较,fastjson 比 json要至少慢5倍

前端之家收集整理的这篇文章主要介绍了纯字符串assemble和parser比较,fastjson 比 json要至少慢5倍前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. package com.xhtt.ifserver.nio;
  2.  
  3. import com.alibaba.fastjson.JSON;
  4.  
  5. import org.json.JSONArray;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8. import org.junit.Test;
  9.  
  10. /**
  11. * auth: WenYF
  12. * date: 2016/11/26
  13. */
  14. public class JsonPerformenceTest {
  15. static int max = 1000;
  16. static int arrayMax = 100;
  17.  
  18. private static String str;
  19. static {
  20. JSONObject json = new JSONObject();
  21. try {
  22. for (int i = 0; i < max ; i ++) {
  23. json.put("key" + i,"valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue" + i);
  24. }
  25. JSONArray array = new JSONArray();
  26. for (int i = 0; i < arrayMax; i++) {
  27. array.put("arrayarrayarrayarrayarray" + i);
  28. }
  29.  
  30. json.put("array",array);
  31. } catch (JSONException e) {
  32. e.printStackTrace();
  33. }
  34. System.out.println(json.toString());
  35. str = json.toString();
  36. }
  37.  
  38. @Test
  39. public void testJsonCreate() {
  40. JSONObject json = new JSONObject();
  41. try {
  42. for (int i = 0; i < max ; i ++) {
  43. json.put("key" + i,"valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue" + i);
  44. }
  45.  
  46. JSONArray array = new JSONArray();
  47. for (int i = 0; i < arrayMax; i++) {
  48. array.put("arrayarrayarrayarrayarray" + i);
  49. }
  50.  
  51. json.put("array",array);
  52. } catch (JSONException e) {
  53. e.printStackTrace();
  54. }
  55. System.out.println(json.toString());
  56. }
  57.  
  58. @Test
  59. public void testFastJsonCreate() {
  60. com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
  61.  
  62. for (int i = 0; i < max ; i ++) {
  63. json.put("key" + i,"valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue" + i);
  64. }
  65.  
  66. com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();
  67. for (int i = 0; i < arrayMax; i++) {
  68. array.add("arrayarrayarrayarrayarray" + i);
  69. }
  70. json.put("array",array);
  71.  
  72. System.out.println(json.toString());
  73. }
  74.  
  75. @Test
  76. public void testJsonParser() {
  77. try {
  78. JSONObject json = new JSONObject(str);
  79. for (int i = 0; i < max ; i ++) {
  80. json.get("key" + i);
  81. }
  82. int i = json.getJSONArray("array").length();
  83. System.out.println("size = " + i);
  84. } catch (JSONException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88.  
  89. @Test
  90. public void testFastJsonParser() {
  91. com.alibaba.fastjson.JSONObject json = JSON.parSEObject(str);
  92. for (int i = 0; i < max ; i ++) {
  93. json.get("key" + i);
  94. }
  95.  
  96. int i = json.getJSONArray("array").size();
  97. System.out.println("size = " + i);
  98. }
  99. }

json.jar | 50c3afb2166798c5de0896b90659d8a2b2f8fcec | http://www.json.org/license.html

fastjson |1.2.21 |https://github.com/alibaba/fastjson

猜你在找的Json相关文章