使用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.compare;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.codehaus.jackson.map.ObjectMapper;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/** * Created by jinx on 7/17/17. */
public class JsonTest {
public static void main(String[] args) throws Exception {
Map<String,String> map = new HashMap<String,String>();
for (int i = 0; i < 100; i++) {
Date date = new Date();
map.put(date.toLocaleString() + i,date.toLocaleString());
}
long startTimestamp = System.currentTimeMillis();
Gson gson = new Gson();
gson.toJson(map);
System.out.println("gson 消耗:" + (System.currentTimeMillis() - startTimestamp));
long startTimestamp2 = System.currentTimeMillis();
JSON.toJSONString(map);
System.out.println("fastjson 1消耗:" + (System.currentTimeMillis() - startTimestamp2));
long startTimestamp3 = System.currentTimeMillis();
JSONObject.toJSONString(map);
System.out.println("fastjson 2消耗:" + (System.currentTimeMillis() - startTimestamp3));
long startTimestamp4 = System.currentTimeMillis();
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(map);
System.out.println("jackson 消耗:" + (System.currentTimeMillis() - startTimestamp4));
}
}
以下是不同数据量的速度比较:
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