cbeust / klaxon

A JSON parser for Kotlin
Apache License 2.0
1.85k stars 121 forks source link

The speed difference with gson #312

Open zhangjiepeng opened 4 years ago

zhangjiepeng commented 4 years ago

var p = Person().apply { name = "h"; age = 10 } var start = System.currentTimeMillis() var gson = Gson() var psg = gson.toJson(p) var p1 = gson.fromJson(psg,Person::class.java) textView.setText("gson cost:"+(System.currentTimeMillis() - start))

        start = System.currentTimeMillis()
        var kl = Klaxon()
        var ksg = kl.toJsonString(p)
        var p2 = kl.parse<Person>(ksg)
        textView.append("\nklaxon cost:"+(System.currentTimeMillis() - start))

gson cost:10 klaxon cost: 1340

xeruf commented 4 years ago

Hey, first some comments :)

  1. Please format the code block appropriately.
  2. Why use a textView instead of simply printing the result?
  3. Please run the test a few (thousand) times before measuring to warm up the JVM
  4. Do it both ways around to ensure that it is not some weird coincidence

If the speed difference persists, this is indeed worrying. 1340ms is more than a second!

contradictioned commented 4 years ago

I also tried a small benchmark with 10k conversions like @zhangjiepeng did for warmup and then 10k conversions where I measured the average duration. Results varied of course, but this was a typical result:

Average duration with GSON: 0.0389ms
Average duration with Klaxon: 0.2669ms
Average duration with Klaxon() instantiated once: 0.1308ms

So the instantiation of the Klaxon object takes half of the parsing time. But still, Gson is an order of magnitude faster.