今天上班优化代码的时候,发现从Redis中取出List
格式的json字符串,通过net.sf.json将其转成List的时候,如果List.size()在1000左右时,消耗处理时间为1000ms,效率非常低;然后搜了下java解析json常用api,发现alibaba的fastJson处理效率还是不错的,当size在1000左右时,也就不足100ms。
然后。。。。回家就简单对比了下几个json处理方式:FastJson、Jackson、Gson、org.Json;
首先maven引入依赖:
<dependency>
<groupId>org.jsongroupId>
<artifactId>jsonartifactId>
<version>20170516version>
dependency>
<dependency>
<groupId>com.google.code.gsongroupId>
<artifactId>gsonartifactId>
<version>2.8.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.35version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.0.pr4version>
dependency>
编写相应的类,测试jsonStr–list,list–jsonStr的处理时间
package com.love.yu.maven.learn.json;
import java.util.List;
import org.json.JSONArray;
public class TestJson {
public void testJsonStr2List(String str) {
long a = System.currentTimeMillis();
JSONArray jsonArray = new JSONArray(str);
List
package com.love.yu.maven.learn.json;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
public class TestFastJson {
public void testFastJsonStr2List(String str) {
long a = System.currentTimeMillis();
JSONArray jsonArray = JSONArray.parseArray(str);
List list = jsonArray.toJavaList(String.class);
long b = System.currentTimeMillis();
System.out.println("FastJsonStr2List:size=" + list.size() + ",time=" + (b - a));
}
public String testFastJsonList2Str(List list) {
long a = System.currentTimeMillis();
String str = JSONArray.toJSONString(list);
long b = System.currentTimeMillis();
System.out.println("FastJsonList2Str:length=" + str.length() + ",time=" + (b - a));
return str;
}
}
package com.love.yu.maven.learn.json;
import java.io.IOException;
import java.util.List;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestJackson {
public void testJacksonStr2List(String str) throws JsonParseException, JsonMappingException, IOException {
long a = System.currentTimeMillis();
ObjectMapper objectMapper = new ObjectMapper();
List list = objectMapper.readValue(str, List.class);
long b = System.currentTimeMillis();
System.out.println("JacksonStr2List:size=" + list.size() + ",time=" + (b - a));
}
public String testJacksonList2Str(List list) throws JsonProcessingException {
long a = System.currentTimeMillis();
ObjectMapper objectMapper = new ObjectMapper();
String str = objectMapper.writeValueAsString(list);
long b = System.currentTimeMillis();
System.out.println("JacksonList2Str:length=" + str.length() + ",time=" + (b - a));
return str;
}
}
package com.love.yu.maven.learn.json;
import java.util.List;
import com.google.gson.Gson;
public class TestGson {
public void testGsonStr2List(String str) {
long a = System.currentTimeMillis();
Gson gson = new Gson();
List list = gson.fromJson(str, List.class);
long b = System.currentTimeMillis();
System.out.println("GsonStr2List:size=" + list.size() + ",time=" + (b - a));
}
public String testGsonList2Str(List list) {
long a = System.currentTimeMillis();
Gson gson = new Gson();
String str = gson.toJson(list);
long b = System.currentTimeMillis();
System.out.println("GsonList2Str:length=" + str.length() + ",time=" + (b - a));
return str;
}
}
最后编写测试类(Main代替)
package com.love.yu.maven.learn.json;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.love.yu.maven.learn.enums.CityEnum;
import com.love.yu.maven.learn.util.Enums;
public class CompareJson {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
List list = new ArrayList();
for (int i = 0; i < 5000000; i++) {
String name = Enums.random(CityEnum.class).name();
list.add(name + "-" + i);
}
TestJson testJson = new TestJson();
for (int i = 0; i < 10; i++) {
String str = testJson.testJsonList2Str(list);
testJson.testJsonStr2List(str);
}
System.out.println("------------");
TestFastJson fastJson = new TestFastJson();
for (int i = 0; i < 10; i++) {
String str = fastJson.testFastJsonList2Str(list);
fastJson.testFastJsonStr2List(str);
}
System.out.println("------------");
TestJackson testJackson = new TestJackson();
for (int i = 0; i < 10; i++) {
String str = testJackson.testJacksonList2Str(list);
testJackson.testJacksonStr2List(str);
}
System.out.println("------------");
TestGson testGson = new TestGson();
for (int i = 0; i < 10; i++) {
String str = testGson.testGsonList2Str(list);
testGson.testGsonStr2List(str);
}
}
}
以size=5000000的list测试转换效率,结果如下
JsonList2Str:length=93890636,time=2060
JsonStr2List:size=5000000,time=4272
JsonList2Str:length=93890636,time=2646
JsonStr2List:size=5000000,time=3270
JsonList2Str:length=93890636,time=2512
JsonStr2List:size=5000000,time=4024
JsonList2Str:length=93890636,time=2366
JsonStr2List:size=5000000,time=3918
JsonList2Str:length=93890636,time=2386
JsonStr2List:size=5000000,time=3977
JsonList2Str:length=93890636,time=2323
JsonStr2List:size=5000000,time=3928
JsonList2Str:length=93890636,time=2352
JsonStr2List:size=5000000,time=4019
JsonList2Str:length=93890636,time=2355
JsonStr2List:size=5000000,time=4001
JsonList2Str:length=93890636,time=2342
JsonStr2List:size=5000000,time=3913
JsonList2Str:length=93890636,time=2401
JsonStr2List:size=5000000,time=4152
------------
FastJsonList2Str:length=93890636,time=2269
FastJsonStr2List:size=5000000,time=747
FastJsonList2Str:length=93890636,time=2150
FastJsonStr2List:size=5000000,time=646
FastJsonList2Str:length=93890636,time=2032
FastJsonStr2List:size=5000000,time=673
FastJsonList2Str:length=93890636,time=2020
FastJsonStr2List:size=5000000,time=652
FastJsonList2Str:length=93890636,time=2149
FastJsonStr2List:size=5000000,time=639
FastJsonList2Str:length=93890636,time=2015
FastJsonStr2List:size=5000000,time=639
FastJsonList2Str:length=93890636,time=2050
FastJsonStr2List:size=5000000,time=632
FastJsonList2Str:length=93890636,time=1998
FastJsonStr2List:size=5000000,time=639
FastJsonList2Str:length=93890636,time=2175
FastJsonStr2List:size=5000000,time=630
FastJsonList2Str:length=93890636,time=2011
FastJsonStr2List:size=5000000,time=624
------------
JacksonList2Str:length=93890636,time=1989
JacksonStr2List:size=5000000,time=671
JacksonList2Str:length=93890636,time=666
JacksonStr2List:size=5000000,time=1763
JacksonList2Str:length=93890636,time=615
JacksonStr2List:size=5000000,time=1843
JacksonList2Str:length=93890636,time=412
JacksonStr2List:size=5000000,time=1643
JacksonList2Str:length=93890636,time=651
JacksonStr2List:size=5000000,time=1727
JacksonList2Str:length=93890636,time=625
JacksonStr2List:size=5000000,time=1755
JacksonList2Str:length=93890636,time=594
JacksonStr2List:size=5000000,time=1584
JacksonList2Str:length=93890636,time=647
JacksonStr2List:size=5000000,time=1704
JacksonList2Str:length=93890636,time=651
JacksonStr2List:size=5000000,time=1817
JacksonList2Str:length=93890636,time=606
JacksonStr2List:size=5000000,time=1626
------------
GsonList2Str:length=93890636,time=2244
GsonStr2List:size=5000000,time=708
GsonList2Str:length=93890636,time=2031
GsonStr2List:size=5000000,time=2133
GsonList2Str:length=93890636,time=2091
GsonStr2List:size=5000000,time=688
GsonList2Str:length=93890636,time=2078
GsonStr2List:size=5000000,time=1721
GsonList2Str:length=93890636,time=2150
GsonStr2List:size=5000000,time=648
GsonList2Str:length=93890636,time=2211
GsonStr2List:size=5000000,time=2679
GsonList2Str:length=93890636,time=1974
GsonStr2List:size=5000000,time=679
GsonList2Str:length=93890636,time=2079
GsonStr2List:size=5000000,time=1706
GsonList2Str:length=93890636,time=2124
GsonStr2List:size=5000000,time=647
GsonList2Str:length=93890636,time=2103
GsonStr2List:size=5000000,time=2604
发现:
疑惑:
gson在处理Str—>List的时候,处理时间交替出现快-慢-快-慢的情况