JSON解释速度对比 - Which parser is fastest on Android?

来源:互联网 发布:淘宝纯棉尿布 编辑:程序博客网 时间:2024/06/06 03:02

Correctness and Performance benchmark for various Java-based JSON libraries

Which parser is fastest on Android?

Here's a quick overview from the implementor of Android's two JSON parsers:

  • org.json The API requires you to load the entire document into a String. This is somewhat cumbersome to use and inefficient for large documents. But it's a DOM model so you can get a lot done with very little code.
  • android.util.JsonReader is a basic pull parser. It has a concise API and is reasonably efficient. It's included in Android 3.0. For earlier Android releases it's available as google-gson-stream which offers the same API in a 14 KiB .jar file. The larger GSON library also offers a DOM API and easy databinding.
  • Jackson is efficient and fully-featured. Jackson has a capable but large API. It accomplishes great efficiency by going to some extremes, like implementing its own floating point parsing and charset decoding. If you use Jackson, you may want to use ProGuard to shrink its 222 KiB jar.

As for hard numbers, Jackson has the edge:

  16 Kb Google Reader data:  JsonReader 2.3 MBps  Jackson 3.8 MBps  68 Kb Twitter data:  JsonReader 2.2 MBps  Jackson 5 MBps  185 Kb Google Reader data:  JsonReader 2.5 MBps  Jackson 6.5 MBps

Some of the optimizations currently employed by Jackson may be coming to Android.


----------------------------------------------------------------

Jackson 首页 : http://jackson.codehaus.org/Home


下面有一篇更详尽的数据比较,Jackson的优势是明显的:

http://martinadamek.com/2011/02/04/json-parsers-performance-on-android-with-warmup-and-multiple-iterations/


JSON Parsers Performance on Android (With Warmup and Multiple Iterations)

FEB 4TH, 2011

My recent posts on JSON parsers performance on Android platform were followed by some nice feedback. But to be honest, the previous version of the code was not exactly optimal. As @cowtowncoder pointed out in comments, there were several problems.

First and most stupid was that I was using very poor implementation of input reading for Android built-in parser. Now after change in the code, this parser is no longer the slowest one. Next problem was that I did not do any warm-up before actual test were executed. Next Tatu suggested to try to run tests in multiple iterations, so I am running them now 1, 5 and 100 times. I’m also using Jackson factory as static singleton, which is recommended.

Now, let’s see actual results as they appear now:

So it’s Jackson, what you should use if you need really fast reading of JSON data. All the code is up on GitHub.


最终实测:大数据上,jackson比Gson快30%左右,大数据量