在Java里面,我们经常使用JSON格式的工具包对字符串或者对象进行解析,一般用得比较广泛的三种分别为:fastJson,jackJson,Gson,关于各个工具包的性能比较网络上比比皆是,在这里我只阐述在我本机环境下的测试结果,然后在根据结果对三种工具包进行一个解析,首先先贴代码:
import java.util.Map; import org.codehaus.jackson.map.ObjectMapper; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; public class JsonTest1 { private static long count=10000; /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub gson(); System.gc(); jackJson(); System.gc(); fastJson(); System.gc(); System.out.println("---------------------------------------------------------------"); gsonJson(); System.gc(); jackJsonJson(); System.gc(); fastJsonJson(); } private static long gson(){ Demo demo=new Demo(); demo.setCmd("1"); demo.setContent("dd"); long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ Gson gson=new Gson(); gson.toJson(demo); } long endTime2=System.currentTimeMillis(); System.out.println("gson--obj--json:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } private static long jackJson() throws Exception{ Demo demo=new Demo(); demo.setCmd("1"); demo.setContent("dd"); long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writeValueAsString(demo); } long endTime2=System.currentTimeMillis(); System.out.println("jackJson--obj--json:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } private static long fastJson(){ Demo demo=new Demo(); demo.setCmd("1"); demo.setContent("dd"); long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ JSONObject jsonObject=new JSONObject(); jsonObject.toJSON(demo); } long endTime2=System.currentTimeMillis(); System.out.println("fastJson--obj--json:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } private static long gsonJson(){ String json="{\"cmd\":\"1\",\"sss\":\"dd\"}"; long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ Gson gson=new Gson(); gson.fromJson(json,Map.class); } long endTime2=System.currentTimeMillis(); System.out.println("gson--json--obj:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } private static long jackJsonJson() throws Exception{ String json="{\"cmd\":\"1\",\"sss\":\"dd\"}"; long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ ObjectMapper objectMapper = new ObjectMapper(); objectMapper.readValue(json, Map.class); } long endTime2=System.currentTimeMillis(); System.out.println("jackJson--json--obj:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } private static long fastJsonJson(){ String json="{\"cmd\":\"1\",\"sss\":\"dd\"}"; long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ JSONObject jsonObject=new JSONObject(); jsonObject.parseObject(json, Map.class); } long endTime2=System.currentTimeMillis(); System.out.println("fastJson--json--obj:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } } class Demo { private String cmd; private String content; public String getCmd() { return cmd; } public void setCmd(String cmd) { this.cmd = cmd; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
运行结果如下:
我们可以看到fastJson的速度明显高于其他两个,真的是这样的吗?我们把代码在改进一下。
import java.util.Map; import org.codehaus.jackson.map.ObjectMapper; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; public class JsonTest { private static ObjectMapper objectMapper = new ObjectMapper(); private static Gson gson=new Gson(); private static JSONObject jsonObject=new JSONObject(); private static long count=10000; /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub gson(); System.gc(); jackJson(); System.gc(); fastJson(); System.gc(); System.out.println("---------------------------------------------------------------"); gsonJson(); System.gc(); jackJsonJson(); System.gc(); fastJsonJson(); } private static long gson(){ Demo demo=new Demo(); demo.setCmd("1"); demo.setContent("dd"); long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ gson.toJson(demo); } long endTime2=System.currentTimeMillis(); System.out.println("gson--obj--json:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } private static long jackJson() throws Exception{ Demo demo=new Demo(); demo.setCmd("1"); demo.setContent("dd"); long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ objectMapper.writeValueAsString(demo); } long endTime2=System.currentTimeMillis(); System.out.println("jackJson--obj--json:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } private static long fastJson(){ Demo demo=new Demo(); demo.setCmd("1"); demo.setContent("dd"); long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ jsonObject.toJSON(demo); } long endTime2=System.currentTimeMillis(); System.out.println("fastJson--obj--json:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } private static long gsonJson(){ String json="{\"cmd\":\"1\",\"sss\":\"dd\"}"; long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ gson.fromJson(json,Map.class); } long endTime2=System.currentTimeMillis(); System.out.println("gson--json--obj:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } private static long jackJsonJson() throws Exception{ String json="{\"cmd\":\"1\",\"sss\":\"dd\"}"; long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ objectMapper.readValue(json, Map.class); } long endTime2=System.currentTimeMillis(); System.out.println("jackJson--json--obj:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } private static long fastJsonJson(){ String json="{\"cmd\":\"1\",\"sss\":\"dd\"}"; long endTime1=System.currentTimeMillis(); for(int i=0;i<count;i++){ jsonObject.parseObject(json, Map.class); } long endTime2=System.currentTimeMillis(); System.out.println("fastJson--json--obj:"+(count*1000)/(endTime2-endTime1)); return endTime2-endTime1; } } class Demo { private String cmd; private String content; public String getCmd() { return cmd; } public void setCmd(String cmd) { this.cmd = cmd; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
运行结果如下:
看到差异了没?上面的第一部分的代码是每次都要去new新对象出来,在这点上导致了fastJson有绝对的优势,而当我们初始化的时候就把该准备的资源准备好呢?fastJson的优势一下就体现不出来了,经过多次测试发现最快的是gson。而且根据上面的结果我们可以看出来fastJson的优势是在json转化为对象的时候比较快,而对象转化为json格式的字符串的时候性能上是没什么优势的,反而是最差的。
结论:
在做了初始化的情况下GSON的速度都是最快的。
在使用GSON或jackJson做Json处理时最好有个全局的解析对象,不要每次调用的时候都去生成那样对性能的影响很大,另外经过测试,这里面没有线程安全问题。
fastJson在json转化为对象的应用场景下性能比较突出
本次的jar版本为:
fastJson版本为1.1.41
GSON:2.1
O