JSON性能测试
在项目开发中,经常会用到JSON来传输数据。JSON工具有多种,如Gson,fastjson, org.json等。那么它们各自的性能如何呢?
这里针对fastjson,Gson,org.json进行了测试,以测试结果为准。测试方法
*测试设备Samsung SM-A9000 android 6.0.1
*主要分析各个JSON工具解析数据的速度*每组测试3次
1,10组时耗时对比。
JSON: {"data":[
{"address":"address_0","id":0,"image":"image_0","name":"name_0"},
{"address":"address_1","id":1,"image":"image_1","name":"name_1"},
{"address":"address_2","id":2,"image":"image_2","name":"name_2"},
{"address":"address_3","id":3,"image":"image_3","name":"name_3"},
{"address":"address_4","id":4,"image":"image_4","name":"name_4"},
{"address":"address_5","id":5,"image":"image_5","name":"name_5"},
{"address":"address_6","id":6,"image":"image_6","name":"name_6"},
{"address":"address_7","id":7,"image":"image_7","name":"name_7"},
{"address":"address_8","id":8,"image":"image_8","name":"name_8"},
{"address":"address_9","id":9,"image":"image_9","name":"name_9"}
],"result":1}
2,100组时耗时对比。
3,500组时耗时对比。
4,5000组时耗时对比。
5,10000组时耗时对比。
6,20000组时耗时对比。
7,50000组时耗时对比。
8,100000组时耗时对比。
9,200000组时耗时对比。
附上测试代码
ThreadPool.add(new Runnable() { @Override public void run() { ArrayListlist = new ArrayList<>(); int num = 10;//可修改数量 for (int i = 0 ; i < num ; i ++){ TestJsonBean jsonBean = new TestJsonBean(); jsonBean.setId(i); jsonBean.setName("name_" + i); jsonBean.setImage("image_" + i); jsonBean.setAddress("address_" + i); list.add(jsonBean); } JsonBean jsonBean0 = new JsonBean(); jsonBean0.setResult(1); jsonBean0.setData(list); ILog.e("-------------第 " + (++count) + " 次测试---------------------------------------------"); long startTime = System.currentTimeMillis()/1000; String json = null ;//JSON.toJSONString(list) Gson gson = new Gson(); json = gson.toJson(jsonBean0); long endTime = System.currentTimeMillis()/1000; ILog.e("生成JSON耗时: " + (endTime - startTime) + " 秒"); ILog.e("JSON: " + json); //fastjson try{ long fStartTime = System.currentTimeMillis(); JsonBean jsonBean = JSON.parseObject(json , JsonBean.class); long fEndTime = System.currentTimeMillis(); ILog.e("fastjson耗时: " + (fEndTime - fStartTime) + " 毫秒" + " " + ((fEndTime - fStartTime)/1000) + " 秒" + " " + jsonBean.toString()); }catch (Exception e){ e.printStackTrace(); } //org.json try{ long jStartTime = System.currentTimeMillis(); JSONObject jsonObject0 = new JSONObject(json); int result = jsonObject0.getInt("result"); JSONArray jsonArray = jsonObject0.getJSONArray("data"); JsonBean bean = new JsonBean(); bean.setResult(result); ArrayList jlist = new ArrayList<>(); int size = jsonArray.length(); for (int i = 0 ; i < size ; i++){ JSONObject jsonObject = jsonArray.getJSONObject(i); int id = jsonObject.getInt("id"); String name = jsonObject.getString("name"); String image = jsonObject.getString("image"); String address = jsonObject.getString("address"); TestJsonBean jsonBean = new TestJsonBean(); jsonBean.setId(id); jsonBean.setName(name); jsonBean.setImage(image); jsonBean.setAddress(address); jlist.add(jsonBean); } bean.setData(jlist); long jEndTime = System.currentTimeMillis(); ILog.e("org.json耗时: " + (jEndTime - jStartTime) + " 毫秒" + " " + ((jEndTime - jStartTime)/1000) + " 秒" + " " + bean.toString()); }catch (Exception e){ e.printStackTrace(); } //GSON try{ long gStartTime = System.currentTimeMillis(); Gson gson1 = new Gson(); JsonBean bean = gson1.fromJson(json , JsonBean.class); long gEndTime = System.currentTimeMillis(); ILog.e("GSON耗时: " + (gEndTime - gStartTime) + " 毫秒" + " " + ((gEndTime - gStartTime)/1000) + " 秒" + " " + bean.toString()); }catch (Exception e){ e.printStackTrace(); } } });
结论:
1.性能方面,org.json全面领先,Gson次之,fastjson最后。
2.在少量数据情况下(500组以内),差别不大,对一般项目无影响
3,大数据情况下,最好选择第一种org.json.