使用Gson版本:2.7
fastjson版本:1.2.2 听说后续版本解析map不再使用treemap排序可能效率会有很大提升
jackson版本: 2.8.2
相关jar包在这里:https://github.com/Jinx009/java_bit_program/tree/master/jar
测试程序如下(代码不一定高准确,仅供个人娱乐使用)
package jinx.json@H_403_9@.compare@H_403_9@;@H_403_9@
import com@H_403_9@.alibaba@H_403_9@.fastjson@H_403_9@.JSON@H_403_9@;@H_403_9@
import com@H_403_9@.alibaba@H_403_9@.fastjson@H_403_9@.JSONObject@H_403_9@;@H_403_9@
import com@H_403_9@.google@H_403_9@.gson@H_403_9@.Gson@H_403_9@;@H_403_9@
import org.codehaus@H_403_9@.jackson@H_403_9@.map@H_403_9@.ObjectMapper@H_403_9@;@H_403_9@
import java.util@H_403_9@.Date@H_403_9@;@H_403_9@
import java.util@H_403_9@.HashMap@H_403_9@;@H_403_9@
import java.util@H_403_9@.Map@H_403_9@;@H_403_9@
/** * Created by jinx on 7/17/17. */@H_403_9@
public class JsonTest {
public static void main(String[] args) throws Exception {
Map<String,String> map = new HashMap<String,String>();@H_403_9@
for (int i = 0@H_403_9@; i < 100; i++) {@H_403_9@
Date date = new Date();@H_403_9@
map.put@H_403_9@(date.toLocaleString@H_403_9@() + i,date.toLocaleString@H_403_9@());@H_403_9@
}
long startTimestamp = System.currentTimeMillis@H_403_9@();@H_403_9@
Gson gson = new Gson();@H_403_9@
gson.toJson@H_403_9@(map);@H_403_9@
System.out@H_403_9@.println@H_403_9@("gson 消耗:"@H_403_9@ + (System.currentTimeMillis@H_403_9@() - startTimestamp));@H_403_9@
long startTimestamp2 = System.currentTimeMillis@H_403_9@();@H_403_9@
JSON.toJSONString@H_403_9@(map);@H_403_9@
System.out@H_403_9@.println@H_403_9@("fastjson 1消耗:"@H_403_9@ + (System.currentTimeMillis@H_403_9@() - startTimestamp2));@H_403_9@
long startTimestamp3 = System.currentTimeMillis@H_403_9@();@H_403_9@
JSONObject.toJSONString@H_403_9@(map);@H_403_9@
System.out@H_403_9@.println@H_403_9@("fastjson 2消耗:"@H_403_9@ + (System.currentTimeMillis@H_403_9@() - startTimestamp3));@H_403_9@
long startTimestamp4 = System.currentTimeMillis@H_403_9@();@H_403_9@
ObjectMapper mapper = new ObjectMapper();@H_403_9@
mapper.writeValueAsString@H_403_9@(map);@H_403_9@
System.out@H_403_9@.println@H_403_9@("jackson 消耗:"@H_403_9@ + (System.currentTimeMillis@H_403_9@() - startTimestamp4));@H_403_9@
}
}
以下是不同数据量的速度比较:
200万循环下(可能由于机器性能,200万数据可能有偏差):
gson 消耗:2154
fastjson 1消耗:2880
fastjson 2消耗:1679
jackson 消耗:1886
gson 消耗:2202
fastjson 1消耗:2888
fastjson 2消耗:1762
jackson 消耗:1917
100万循环下:
gson 消耗:362
fastjson 1消耗:1161
fastjson 2消耗:833
jackson 消耗:400
gson 消耗:373
fastjson 1消耗:1140
fastjson 2消耗:838
jackson 消耗:402
gson 消耗:379
fastjson 1消耗:1137
fastjson 2消耗:892
jackson 消耗:477
10万循环下:
gson 消耗:148
fastjson 1消耗:393
fastjson 2消耗:104
jackson 消耗:187
gson 消耗:146
fastjson 1消耗:402
fastjson 2消耗:101
jackson 消耗:188
1万循环下:
gson 消耗:114
fastjson 1消耗:298
fastjson 2消耗:30
jackson 消耗:219
gson 消耗:94
fastjson 1消耗:294
fastjson 2消耗:26
jackson 消耗:164
gson 消耗:106
fastjson 1消耗:327
fastjson 2消耗:29
jackson 消耗:212
100循环下:
gson 消耗:67
fastjson 1消耗:253
fastjson 2消耗:2
jackson 消耗:216
gson 消耗:61
fastjson 1消耗:259
fastjson 2消耗:2
jackson 消耗:160
gson 消耗:67
fastjson 1消耗:240
fastjson 2消耗:1
jackson 消耗:154