首页 > 代码库 > FastJson与Gson小测试

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 用时:1188FastJson to JSON 用时:1034FastJson to JSON 用时:1201FastJson to JSON 用时:1450FastJson to JSON 用时:712FastJson to JSON 用时:1156FastJson to JSON 用时:695FastJson to JSON 用时:1142FastJson to JSON 用时:680FastJson to JSON 用时:1206Gson   to   JSON 用时:1079Gson   to   JSON 用时:1108Gson   to   JSON 用时:905Gson   to   JSON 用时:1097Gson   to   JSON 用时:903Gson   to   JSON 用时:1056Gson   to   JSON 用时:905Gson   to   JSON 用时:903Gson   to   JSON 用时:1113Gson   to   JSON 用时:897
JSON to LIST 用时比较  FastJson to List 用时:351FastJson to List 用时:222FastJson to List 用时:189FastJson to List 用时:177FastJson to List 用时:281FastJson to List 用时:349FastJson to List 用时:258FastJson to List 用时:226FastJson to List 用时:213FastJson to List 用时:201Gson   to   List 用时:1305Gson   to   List 用时:745Gson   to   List 用时:790Gson   to   List 用时:864Gson   to   List 用时:886Gson   to   List 用时:1168Gson   to   List 用时:907Gson   to   List 用时:796Gson   to   List 用时:914Gson   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


不足之处请大家指点 . . .

fastJson 下载:链接: http://pan.baidu.com/s/1kT7hVnx  

 gson     下载:链接: http://pan.baidu.com/s/1sjLiQRF  

FastJson与Gson小测试