FastJson與Gson小測試



最近用到Json來傳輸數據,找到兩個比較簡單的工具 Gson 和 FastJson
隨便測試一下兩個工具的效率~


1
package com.json.fast; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.alibaba.fastjson.JSON; 7 import com.demo.module.Student; 8 import com.google.gson.Gson; 9 import com.google.gson.reflect.TypeToken; 10 11 12 public class FastJson { 13 public static void main(String[] args) { 14 15 16 List<Student> list=new ArrayList<Student>(); 17 18 for (int i = 0; i < 500000; i++) { 19 Student student=new Student(); 20 student.setId(i); 21 student.setName("Name"+i); 22 student.setAge(i); 23 student.setSex("boy"); 24 25 list.add(student); 26 } 27 28 String json=JSON.toJSONString(list); 29 for (int i = 0; i < 10; i++) { 30 long time_start = System.currentTimeMillis(); 31 JSON.toJSONString(list); 32 long time_end = System.currentTimeMillis(); 33 System.out.println("FastJson to JSON 用時:"+(time_end-time_start)); 34 } 35 System.out.println(); 36 for (int i = 0; i < 10; i++) { 37 long time_start = System.currentTimeMillis(); 38 JSON.parseArray(json, Student.class); 39 long time_end = System.currentTimeMillis(); 40 System.out.println("FastJson to List 用時:"+(time_end-time_start)); 41 } 42 System.out.println(); 43 44 Gson gson=new Gson(); 45 for (int i = 0; i < 10; i++) { 46 long time_startg = System.currentTimeMillis(); 47 gson.toJson(list); 48 long time_endg = System.currentTimeMillis(); 49 System.out.println("Gson to JSON 用時:"+(time_endg-time_startg)); 50 } 51 System.out.println(); 52 for (int i = 0; i < 10; i++) { 53 long time_startg = System.currentTimeMillis(); 54 gson.fromJson(json,new TypeToken<List<Student>>(){}.getType()); 55 long time_endg = System.currentTimeMillis(); 56 System.out.println("Gson to List 用時:"+(time_endg-time_startg)); 57 } 58 } 59 }

 

 

LIST to JSON 用時比較 
FastJson to JSON 用時:1188
FastJson to JSON 用時:1034
FastJson to JSON 用時:1201
FastJson to JSON 用時:1450
FastJson to JSON 用時:712
FastJson to JSON 用時:1156
FastJson to JSON 用時:695
FastJson to JSON 用時:1142
FastJson to JSON 用時:680
FastJson to JSON 用時:1206

Gson   to   JSON 用時:1079
Gson   to   JSON 用時:1108
Gson   to   JSON 用時:905
Gson   to   JSON 用時:1097
Gson   to   JSON 用時:903
Gson   to   JSON 用時:1056
Gson   to   JSON 用時:905
Gson   to   JSON 用時:903
Gson   to   JSON 用時:1113
Gson   to   JSON 用時:897
JSON to LIST 用時比較 
 
FastJson to List 用時:351
FastJson to List 用時:222
FastJson to List 用時:189
FastJson to List 用時:177
FastJson to List 用時:281
FastJson to List 用時:349
FastJson to List 用時:258
FastJson to List 用時:226
FastJson to List 用時:213
FastJson to List 用時:201

Gson   to   List 用時:1305
Gson   to   List 用時:745
Gson   to   List 用時:790
Gson   to   List 用時:864
Gson   to   List 用時:886
Gson   to   List 用時:1168
Gson   to   List 用時:907
Gson   to   List 用時:796
Gson   to   List 用時:914
Gson   to   List 用時:1206
 1 package com.demo.module;
 2 
 3 public class Student {
 4     private int id;
 5     private String name;
 6     private int age;
 7     private String sex;
 8     
 9     public int getId() {
10         return id;
11     }
12     public void setId(int id) {
13         this.id = id;
14     }
15     public String getName() {
16         return name;
17     }
18     public void setName(String name) {
19         this.name = name;
20     }
21     public int getAge() {
22         return age;
23     }
24     public void setAge(int age) {
25         this.age = age;
26     }
27     public String getSex() {
28         return sex;
29     }
30     public void setSex(String sex) {
31         this.sex = sex;
32     }
33     
34 }
View Code


不足之處請你們指點 . . . java

fastJson 下載:連接: http://pan.baidu.com/s/1kT7hVnx   json

 gson     下載:連接: http://pan.baidu.com/s/1sjLiQRF   ide

相關文章
相關標籤/搜索