杰克逊和谷歌的Gson之间的JSON解析性能比较

6

在谷歌上搜索后,发现jackson比gson性能更好,我计划在我的项目中用jackson替换gson,但是当我运行测试代码时得到了不同的结果。

private static final Type PHOTOLINKS_TYPE_GSON = new TypeToken<List<Photo>>() {}.getType();
private static final Type PHOTOCAPTIONS_TYPE_GSON = new TypeToken<List<String>>() {}.getType();
Gson gson = new Gson();
private void testGson(String photoJson, String captionJson) {
    GSON_MON.start();
    List<Photo> photos = gson.fromJson(photoJson, PHOTOLINKS_TYPE_GSON);
    List<String> photoCaptions = gson.fromJson(captionJson, PHOTOCAPTIONS_TYPE_GSON);
    GSON_MON.stop();
}

TypeReference<List<Photo>> PHOTOLINKS_TYPE_JACKSON = new TypeReference<List<Photo>>(){};
TypeReference<List<String>> PHOTOCAPTIONS_TYPE_JACKSON = new TypeReference<List<String>>(){};
ObjectMapper mapper = new ObjectMapper();
private void testJackson(String photoJson, String captionJson) {
    JACKSON_MON.start();
    try {
        List<Photo> photos = mapper.readValue(photoJson, PHOTOLINKS_TYPE_JACKSON);
        List<String> photoCaptions = mapper.readValue(captionJson, PHOTOCAPTIONS_TYPE_JACKSON);
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    JACKSON_MON.stop();
}

Photo是一个普通的类:

@JsonIgnoreProperties(ignoreUnknown = true)
private static class Photo implements Serializable {
private static final long serialVersionUID = 5645393489907650496L;

public String small;
public String middle;
public String orign;
public String caption;
public String ow;
public String oh;
}

照片的JSON格式如下:

[{"id":"1318403074887","orign":"xxx.jpg","ow":427,"small":"xxx.jpg","middle":"xxx.jpg","oh":640},{"id":"1318403076793","orign":"xxx.jpg","ow":640,"small":"xxx.jpg","middle":"xxx.jpg","oh":480},{"id":"1318403092168","orign":"xxx.jpg","ow":425,"small":"xxx.jpg","middle":"xxx.jpg","oh":640}]

我使用JAMon来监控性能,以下是结果:

  • JAMon Label=jackson,Units=ms.:(LastValue=18.0,Hits=30.0,Avg=18.4,Total=552.0,Min=13.0,Max=37.0,Active=0.0,Avg Active=1.0,Max Active=1.0)
  • JAMon Label=gson,Units=ms.:(LastValue=4.0,Hits=30.0,Avg=2.1666666666666665,Total=65.0,Min=0.0,Max=4.0,Active=0.0,Avg Active=1.0,Max Active=1.0)
  • JAMon Label=jackson,Units=ms.:(LastValue=20.0,Hits=30.0,Avg=15.166666666666666,Total=455.0,Min=12.0,Max=25.0,Active=0.0,Avg Active=1.0,Max Active=1.0)
  • JAMon Label=gson,Units=ms.:(LastValue=4.0,Hits=30.0,Avg=2.2,Total=66.0,Min=0.0,Max=9.0,Active=0.0,Avg Active=1.0,Max Active=1.0)
  • JAMon Label=jackson,Units=ms.:(LastValue=19.0,Hits=30.0,Avg=16.433333333333334,Total=493.0,Min=11.0,Max=51.0,Active=0.0,Avg Active=1.0,Max Active=1.0)
  • JAMon Label=gson,Units=ms.:(LastValue=2.0,Hits=30.0,Avg=1.9,Total=57.0,Min=0.0,Max=6.0,Active=0.0,Avg Active=1.0,Max Active=1.0)

看起来gson比jackson更快,gson的平均时间约为2ms,而jackson的平均时间约为16ms。我在使用jackson时是否犯了错误?

1个回答

5

可能是性能监测的简单问题:看起来您没有通过运行测试来"热身"JVM,以便让它编译字节码等。通常,在进行测量之前,测试需要至少运行5-10秒钟。

因此,首先尝试这样做,看看数字如何变化。我敢打赌两者的数字都会增加--对于小对象,应该只需要几毫秒。


2
是的,你说得对。 我再次运行测试,并使用纳秒进行监测, Jackson 比 Gson 更快: Jackson 总共:4742510320 纳秒,平均:4742510 纳秒 Gson 总共:13498619947 纳秒,平均:13498619 纳秒Jackson 总共:7667802989 纳秒,平均:7667802 纳秒 Gson 总共:25132581619 纳秒,平均:25132581 纳秒 - situch
+1 对于这个信息,我也遇到了类似的情况,在运行测试多次后,执行时间减少了4倍。 - Juan Antonio Gomez Moriano

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接