Now there is a lot of third-party parsing of json, such as json-lib, gson, jackson, fastjson and so on. When we do the general json-object conversion work, there are almost no problems.
But when the amount of data comes up, what is their performance? I started to test gson and jackson. Others such as json-lib have poor performance. Although fastjson has good performance, there are some problems when using it, so there is no test here.
Short code:
//Generate larger json
List list = new ArrayList();
for (int i = 0; i < 500000; i++) {
JsonObject obj = new JsonObject();
obj.setId(i);
obj.setName ("name" + String.valueOf(i));
list.add(obj);
}
Gson gson = new GsonBuilder().create();
String str = gson.toJson(list);
//1, gson parses
long start1 = System.currentTimeMillis();
List l = gson.fromJson(str, new TypeToken>() {
}.getType());
System.out.println("gson time elapse:" + (System.currentTimeMillis() - start1));
System.out.println(l.size());
//2,jackson解析
ObjectMapper mapper = new ObjectMapper();
long start2 = System.currentTimeMillis();
List l2 = mapper.readValue(str, new TypeReference>() {
});
System.out.println("jackson time elapse:" + (System.currentTimeMillis() - start2));
System.out.println(l2.size());
测试结果:
数据集 gson耗时 Jackson耗时
10w 1366 138
20w 2720 165
30w 4706 332
40w 9526 317
50w Native OOM 363
From the test results, it can be seen that the performance of jackson is almost 10 times that of gson, and as the data grows, the time-consuming of jackson is also very stable, while that of gson The time-consuming increase obviously, and finally OOM directly.
As for why jackson's performance is so good, I have not studied it in detail. It may be because jackson uses the stream processing method.
Reference:
http://blog.itpub.net/28912557/viewspace-1267965/
http://blog.csdn.net/u014315849/article/details/52126653
JSON parsing performance comparison (gson and jackson)
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326608717&siteId=291194637
Recommended
Ranking