標籤:color 工作 rtt 更新 速度 gson bcd string alibaba
使用版本:
compile ‘com.google.code.gson:gson:2.7‘ compile ‘com.alibaba:fastjson:1.2.17‘
評測樣板為一個People數組,People對象 中包含一個Food對象引用。各個字串採用隨機數類比;盡量類比列表請求資料。
String mString = "abcdefghijklmnopqrstuvwxyz0123456789";
Random mRandom = new Random();
public List<People> createPeopleList(int n){ List<People> list = new ArrayList<>(); for (int i=0; i< n ; i++){ list.add(createPeople()); } return list; } public People createPeople(){ People people = new People(); people.name = getRandomString(); people.age = Math.abs(mRandom.nextInt()) % 100; people.food = createFood(); return people; } public Food createFood(){ Food food = new Food(); food.name = getRandomString(); food.num = Math.abs(mRandom.nextInt()) % 10; food.price = Math.abs(mRandom.nextInt()) % 100; return food; } public String getRandomString(){ int size = Math.abs(mRandom.nextInt()) % 6; String str = ""; int len = mString.length(); for(int i=0; i< size; i++){ str += mString.charAt(Math.abs(mRandom.nextInt()) % len); } return str; }
評測Demo:
public String testToJson(int n){ mText = ""; List list = createPeopleList(n); long startTime = System.currentTimeMillis(); Gson gson = new Gson(); long initTime = System.currentTimeMillis(); String gsonStr = gson.toJson(list); long parseTime = System.currentTimeMillis(); String gsonTime = "gson to initTime: "+(initTime - startTime) +" parse: "+(parseTime - initTime); startTime = System.currentTimeMillis(); String fastStr = JSON.toJSON(list).toString(); parseTime = System.currentTimeMillis(); String fastTime = "fast to parse: "+(parseTime - startTime); mText = gsonTime + "\n" + fastTime;// Log.d("tag", gsonStr);// Log.d("tag", fastStr); return gsonStr; } public Object testFromJson(String str){ List<People> list = new ArrayList<>(); long startTime = System.currentTimeMillis(); Gson gson = new Gson(); long initTime = System.currentTimeMillis(); list = gson.fromJson(str, list.getClass()); long parseTime = System.currentTimeMillis(); String gsonTime = "gson from initTime: "+(initTime - startTime) +" parse: "+(parseTime - initTime); startTime = System.currentTimeMillis(); list = JSON.parseObject(str, list.getClass()); parseTime = System.currentTimeMillis(); String fastTime = "fast from parse: "+(parseTime - startTime); mText += "\n"+gsonTime + "\n" + fastTime; return list; }
評測機型:360 型號1503-M02, 處理器:helio X20 十核處理器, 記憶體4G, 系統6.0, 核心版本3.18.22+
輸出資料:
Size 大小 GSON toJson FastJson toJson GSON fromJson FastJson parseJson 單位(ms)
20 25 39 6 3
30 39 6 2
27 40 6 3
200 22 15 12 11
23 16 13 12
22 15 11 12
2000 116 87 43 61
128 83 72 89
120 85 44 73
20000 610 766 596 666
709 793 525 759
530 910 543 773
200000 6875 15394 11551 18811
6803 15419 10050 18718
6756 15217 11338 19507
資料分析:
1、Size 為 20 的時候 資料偏大是因為有靜態變數等相關的初始化工作,接下來的 200、2000等因為已經初始化了,所以沒有相應增加時間。
2、產生json字串的速度,2000個對象以內,fastJson 有優勢, 20000個資料以上Gson優勢比較大
3、解析json字串的資料, 除了20個樣板的極端例子,Gson 的解析效能都有可觀的優勢。
總結:
1、android開放中,按照以往經驗解析json樣板 不超過2000, 封裝json的樣板不超過200,選擇Gson有一定優勢。
2、FastJson整體表現不如Gson,但是在特定樣板大小中,有更好的效能。
3、GitHub上面FastJson更新比Gson更慢
4、建議使用Gson
5、上述樣板存在局限,沒有覆蓋到很多範例,具體項目中,可以實測之後在選擇方案。
Gson 和 FastJson 效能測試