覚えたら書く

IT関係のデベロッパとして日々覚えたことを書き残したいです。twitter: @yyoshikaw

Gson vs Genson (1)

Java用のJSONライブラリはいくつかありますが、今回GsonGensonの性能を比較してみました。
とりあえず単純なBeanからシリアライズ、Beanへのデシリアライズを実行しました。

基本GsonとGensonの比較なんですが、ついでにJSONICも比較してみました
(なんでJacksonが比較対象に入ってないんだと言われそうですね)


pom.xmlに追加した内容は以下の通り

<dependency>
  <groupId>com.owlike</groupId>
  <artifactId>genson</artifactId>
  <version>1.4</version>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.7</version>
</dependency>
<dependency>
    <groupId>net.arnx</groupId>
    <artifactId>jsonic</artifactId>
    <version>1.3.10</version>
</dependency>

JMHでの測定用のコードは以下です

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import com.google.gson.Gson;
import com.owlike.genson.Genson;

import net.arnx.jsonic.JSON;

public class GsonVSGenson {

    static final Genson genson = new Genson();

    static final Gson gson = new Gson();

    private static final Person person = new Person();

    static {
        person.setName("Taro");
        person.setAge(30);
    }

    @Benchmark
    public void Gson_deserialize() {
        Person person = gson.fromJson("{\"age\":28,\"name\":\"Foo\"}", Person.class);
    }

    @Benchmark
    public void Genson_deserialize() {
        Person person = genson.deserialize("{\"age\":28,\"name\":\"Foo\"}", Person.class);
    }

    @Benchmark
    public void JSONIC_deserialize() {
        Person person = JSON.decode("{\"age\":28,\"name\":\"Foo\"}", Person.class);
    }

    @Benchmark
    public void Gson_serialize() {
        String json = gson.toJson(person);
    }

    @Benchmark
    public void Genson_serialize() {
        String json = genson.serialize(person);
    }

    @Benchmark
    public void JSONIC_serialize() {
        String json = JSON.encode(person);
    }

    static final class Person {

        private String name;

        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                // 実行すべきベンチマークが定義された"クラス名.メソッド名"を正規表現で指定
                .include(GsonVSGenson.class.getSimpleName())
                .warmupIterations(15)
                .forks(1)
                .mode(Mode.Throughput)
                .build();
        new Runner(opt).run();
    }

}

実行結果:

# Run complete. Total time: 00:03:40

Benchmark                         Mode  Cnt       Score      Error  Units
GsonVSGenson.Genson_deserialize  thrpt   20  456319.525 ± 3665.629  ops/s
GsonVSGenson.Genson_serialize    thrpt   20  743057.031 ± 5163.948  ops/s
GsonVSGenson.Gson_deserialize    thrpt   20  586334.682 ± 5144.659  ops/s
GsonVSGenson.Gson_serialize      thrpt   20  956016.504 ± 8477.271  ops/s
GsonVSGenson.JSONIC_deserialize  thrpt   20   89910.339 ±  453.891  ops/s
GsonVSGenson.JSONIC_serialize    thrpt   20  214784.107 ± 1612.983  ops/s

この結果だけ見るとGsonの方が早いってことになりますね。
ただし、JSON文字列の内容等によって結果が異なってくる可能性は十分にあるとは思います。
JSONICはさすがにこの中では一番結果悪いですね。



関連エントリ