使用版本:android
compile 'com.google.code.gson:gson:2.7' compile 'com.alibaba:fastjson:1.2.17'
評測樣板爲一個People數組,People對象 中包含一個Food對象引用。各個字符串採用隨機數模擬;儘可能模擬列表請求數據。json
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+dom
輸出數據:性能
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 122000 116 87 43 61 128 83 72 89 120 85 44 7320000 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 數據分析:一、Size 爲 20 的時候 數據偏大是由於有靜態變量等相關的初始化工做,接下來的 200、2000等由於已經初始化了,因此沒有相應增長時間。二、生成json字符串的速度,2000個對象之內,fastJson 有優點, 20000個數據以上Gson優點比較大三、解析json字符串的數據, 除了20個樣板的極端例子,Gson 的解析性能都有可觀的優點。總結:一、android開放中,按照以往經驗解析json樣板 不超過2000, 封裝json的樣板不超過200,選擇Gson有必定優點。二、FastJson總體表現不如Gson,可是在特定樣板大小中,有更好的性能。三、GitHub上面FastJson更新比Gson更慢四、建議使用Gson五、上述樣板存在侷限,沒有覆蓋到不少樣例,具體項目中,能夠實測以後在選擇方案。