Jackson版本:2.9.1
Gson版本:2.8.2
Fastjson版本:1.2.40
pom.xml加入依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.1</version>
</dependency>
新建Bean类
package com;
public class Book{
private String id;
private String name;
public Book() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Book{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
测试类
package com;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* Created by admin on 2017/11/12.
*/
public class Time {
private final static Logger log = LoggerFactory.getLogger(Time.class);
static void jackJson(String jsonStr, int num) throws IOException {
long start = System.currentTimeMillis();
Book book = new Book();
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
for (int j = 0; j<num; j++){
book = mapper.readValue(jsonStr, Book.class);
}
long end = System.currentTimeMillis();
log.error("jackJson循环 {} 次花费的时间为:{}", num, end-start);
}
static void fastJson(String jsonStr, int num){
long start = System.currentTimeMillis();
Book book = new Book();
for (int j = 0; j<num; j++){
book = JSON.parseObject(jsonStr, Book.class);
}
long end = System.currentTimeMillis();
log.error("fastJson循环 {} 次花费的时间为:{}", num, end-start);
}
static void gson(String jsonStr, int num){ long start = System.currentTimeMillis();
Gson gson = new Gson();
Book book = new Book();
for (int j = 0; j<num; j++){
book = gson.fromJson(jsonStr, Book.class);
}
long end = System.currentTimeMillis();
log.error("Gson循环 {} 次花费的时间为:{}", num, end-start);
}
public static void main(String[] args) throws IOException {
String jsonStr = "{\"id\":\"2\",\"name\":\"Json技术\"}";
fastJson(jsonStr, 50000);
gson(jsonStr, 50000);
jackJson(jsonStr, 50000);
log.error("-----------------------------------------------------");
fastJson(jsonStr, 500000);
gson(jsonStr, 500000);
jackJson(jsonStr, 500000);
log.error("-----------------------------------------------------");
fastJson(jsonStr, 800000);
gson(jsonStr, 800000);
jackJson(jsonStr, 800000);
log.error("-----------------------------------------------------");
fastJson(jsonStr, 2000000);
gson(jsonStr, 2000000);
jackJson(jsonStr, 2000000);
log.error("-----------------------------------------------------");
fastJson(jsonStr, 8000000);
gson(jsonStr, 8000000);
jackJson(jsonStr, 8000000);
log.error("-----------------------------------------------------");
fastJson(jsonStr, 200000000);
gson(jsonStr, 200000000);
jackJson(jsonStr, 200000000);
}
}
测试结果:
解析二十万次(毫秒)
类型 |
第一次 |
第二次 |
第三次 |
FastJson |
239 |
249 |
237 |
Gson |
1170 |
1129 |
1156 |
JackSon |
552 |
527 |
535 |
解析二百万次(毫秒)
类型 |
第一次 |
第二次 |
第三次 |
FastJson |
2845 |
2420 |
2379 |
Gson |
11238 |
11412 |
11247 |
JackSon |
5917 |
5441 |
5468 |
解析二千万次(毫秒)
类型 |
第一次 |
第二次 |
第三次 |
FastJson |
24182 |
|
|
Gson |
102649 |
|
|
JackSon |
54319 |
|
|
结论:
十万次以下,三个解析时间相差不大,
十万次以上的话,三个比例可以为:
Jackson:Gson:Fastjson = 5:11:2
所以无论什么情况下,还是Fastjson解析时间最快。 |