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 }
不足之处请大家指点 . . .
fastJson 下载:链接: http://pan.baidu.com/s/1kT7hVnx
gson 下载:链接: http://pan.baidu.com/s/1sjLiQRF