FastJson与Gson小测试

时间:2022-07-21 17:24:33

最近用到Json来传输数据,找到两个比较简单的工具 Gson 和 FastJson
随便测试一下两个工具的效率~ 1 package com.json.fast; import java.util.ArrayList;
import java.util.List; import com.alibaba.fastjson.JSON;
import com.demo.module.Student;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; public class FastJson {
public static void main(String[] args) { List<Student> list=new ArrayList<Student>(); for (int i = 0; i < 500000; i++) {
Student student=new Student();
student.setId(i);
student.setName("Name"+i);
student.setAge(i);
student.setSex("boy"); list.add(student);
} String json=JSON.toJSONString(list);
for (int i = 0; i < 10; i++) {
long time_start = System.currentTimeMillis();
JSON.toJSONString(list);
long time_end = System.currentTimeMillis();
System.out.println("FastJson to JSON 用时:"+(time_end-time_start));
}
System.out.println();
for (int i = 0; i < 10; i++) {
long time_start = System.currentTimeMillis();
JSON.parseArray(json, Student.class);
long time_end = System.currentTimeMillis();
System.out.println("FastJson to List 用时:"+(time_end-time_start));
}
System.out.println(); Gson gson=new Gson();
for (int i = 0; i < 10; i++) {
long time_startg = System.currentTimeMillis();
gson.toJson(list);
long time_endg = System.currentTimeMillis();
System.out.println("Gson to JSON 用时:"+(time_endg-time_startg));
}
System.out.println();
for (int i = 0; i < 10; i++) {
long time_startg = System.currentTimeMillis();
gson.fromJson(json,new TypeToken<List<Student>>(){}.getType());
long time_endg = System.currentTimeMillis();
System.out.println("Gson to List 用时:"+(time_endg-time_startg));
}
}
}
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
 package com.demo.module;

 public class Student {
private int id;
private String name;
private int age;
private String sex; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} }

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

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

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