Gson,fastjson,jackson效率测试程序
使用Gson版本:2.7 测试程序如下(代码不一定高准确,仅供个人娱乐使用) 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));
}
}
以下是不同数据量的速度比较: gson 消耗:2202 100万循环下: gson 消耗:373 gson 消耗:379 10万循环下: gson 消耗:146 1万循环下: gson 消耗:94 gson 消耗:106 100循环下: gson 消耗:61 gson 消耗:67 由于数据量有限,机器性能有限,只获取了一个结论,小数据使用fastjson的jsonobject转换效率是最高的, (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |