xml、json序列化反序列化时间对比
结果:单位(毫秒)
xml:30810
fastjson:859
gson:1482
源代码:
private void compTime() { Person p = new Person(); p.setAge(23); p.setName("李志鹏"); p.setBirthday(new Date()); long count = 100000; System.out .println("-----------------------compTime-----------------------"); long startMili; long endMili; startMili = System.currentTimeMillis(); for (int i = 0; i < count; i++) { Writer writer = new StringWriter(); JAXB.marshal(p,writer); String xml = writer.toString(); Reader reader = new StringReader(xml); Person retP = JAXB.unmarshal(reader,p.getClass()); } endMili = System.currentTimeMillis(); System.out.println("xml:" + (endMili - startMili)); startMili = System.currentTimeMillis(); for (int i = 0; i < count; i++) { String json = JSON.toJSONString(p); Person retP = JSON.parSEObject(json,new TypeReference<Person>() { }); } endMili = System.currentTimeMillis(); System.out.println("fastjson:" + (endMili - startMili)); startMili = System.currentTimeMillis(); Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss") .create(); for (int i = 0; i < count; i++) { String json = gson.toJson(p); Person retP = gson.fromJson(json,new TypeToken<Person>() { }.getType()); } endMili = System.currentTimeMillis(); System.out.println("gson:" + (endMili - startMili)); }
序列化同一对象形成的数据大小对比:
结果:单位:字节
json:368
xml:568
源代码:
private void compSize() { Map<String,Object> map = TestPrepareData.getStudentAndTeacher(); String json = JSON.toJSONString(map); String xml = XmlJson.json2xml(json); System.out.println("json:" + json); System.out.println("xml:" + xml); System.out.println("json:" + json.length()); System.out.println("xml:" + xml.length()); }原文链接:https://www.f2er.com/xml/299590.html