号称最快的fastjson,经测试发现却是最慢的
最近在找java中json处理库,找了三个常见的类库:fastjson,Gson,jackson在实际使用中,fastjson写法最简单,jackson写法最繁。
看fastjson的论坛,其作者温少号称“ gson性能极差!g并非google的缩写,是龟速的缩写!”(http://www.oschina.net/news/20006/fastjson-1-1-1)
于是写了个测试来测试三种json库的速度。我这里只按我的需求,处理对象序列化和反序列化。
在我的电脑上测试结果如下:
jackson serialize object:1219
fastjson serialize object:1906
Gson serialize object:1516
--------------
jackson parse object:2219
fastjson parse object:18156
Gson parse object:2078
三个类库基本是最新版本:fastjson-1.1.9.jar,gson-2.2.4.jar,jackson-all-1.9.11.jar
如果测试有内存问题,在java运行环境加 -Xmx1024M
测试用例如下:
/**
* @author NieLei E-mail:niles2010@live.cn
* @version create time:2013-7-8 16:25:18
*/
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
public class TestAllJsonlib {
public TestAllJsonlib() {
buildJsonString();
}
private void buildJsonString() {
Group group = new Group();
for(int i=0;i<200000;i++) {
User user = new User();
user.setId(1L);
user.setName("test" + i);
user.setVendor("class" + i);
user.setImageUrl("img/class.png" + i);
user.setCreator("niles" + i);
user.setInfo("Test user in group" + i);
group.addUser(user);
}
try {
System.err.println("start compare...");
Gson gson = new Gson();
ObjectMapper mapper = new ObjectMapper();
long t0 = 0;
long t1 = System.currentTimeMillis();
String str = null;
t0 = t1;
str = mapper.writeValueAsString(group);
t1 = System.currentTimeMillis();
System.err.println("jackson serialize object:" + (t1 - t0));
t0 = t1;
str = JSON.toJSONString(group);
t1 = System.currentTimeMillis();
System.err.println("fastjson serialize object:" + (t1 - t0));
t0 = t1;
str = gson.toJson(group);
t1 = System.currentTimeMillis();
System.err.println("Gson serialize object:" + (t1 - t0));
System.err.println("--------------");
t0 = t1;
Group group2 = mapper.readValue(str, Group.class);
t1 = System.currentTimeMillis();
System.err.println("jackson parse object:" + (t1 - t0));
t0 = t1;
Group group0 = JSON.parseObject(str,Group.class);
t1 = System.currentTimeMillis();
System.err.println("fastjson parse object:" + (t1 - t0));
t0 = t1;
Group group1 = gson.fromJson(str, Group.class);
t1 = System.currentTimeMillis();
System.err.println("Gson parse object:" + (t1 - t0));
} catch(Exception ex) {
ex.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
new TestAllJsonlib();
}
}
class User {
private Long id;
private String name;
private String vendor;
private String imageUrl;
private String creator;
private String info;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setVendor(String vendor) {
this.vendor = vendor;
}
public String getVendor() {
return this.vendor;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getImageUrl() {
return this.imageUrl;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
class Group {
private Map<String,User> users = new Hashtable<String,User>();
private Date updateTime = null;
public Date getUpdateTime() { return updateTime; }
public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; }
public Map<String,User> getUsers() { return users; }
public void setUsers(Map<String,User> users) { this.users = users; }
public void addUser(User user) {
users.put(user.getName(), user);
}
public User getUser(String name) {
return users.get(name);
}
}
结论: 东西好不好,真不用吹啊。 gson fastjson --------------------编程问答-------------------- 和版本有关吧。
如果fastjson 版本是1.1.32时,我这测试结果:
start compare...
jackson serialize object:397
fastjson serialize object:754
Gson serialize object:705
--------------
jackson parse object:1345
fastjson parse object:673
Gson parse object:992
我基本把fastjson每个版本都跑了下,发现1.1.24以前的版本后1.1.24以后的版本parse性能差好几倍,应该是有个重要的改进。
1.1.24以后的版本测试结果,基本能判断fastjson是最快的,2项时间之合。 --------------------编程问答-------------------- 看了楼上的说明,又下载了个最新版本1.1.32,跑一下,新的结果如下:
jackson serialize object:1203
fastjson serialize object:1812
Gson serialize object:1860
--------------
jackson parse object:1890
fastjson parse object:938
Gson parse object:1640
确实,新版本速度快很多,基本三个类库在一个等级上。
--------------------编程问答--------------------
哥们,结贴哦,就差这点分升级了 --------------------编程问答-------------------- 不客观,FASTJSON初始化慢,也就是说第一次慢,以后会很快的。你得写个FOR循环再看看结果。 --------------------编程问答-------------------- 接分了,快结贴吧. --------------------编程问答-------------------- 不客观,应该循环比较
补充:Java , Java相关