14

I am trying to use gson to do my object mapping on the android emulator.

It has been ridiculously slow when processing json data around 208 kb. I do not have any hierarchies in my json.

After the object mapping is done, i can see it that gson created around 500 records.

It is taking it over 3 minutes on the android emulator to map the input json.

I have annotated my entity which comprises of strings and couple of floats.

An I missing something?

Any ideas, best practices would greatly help.

Are there any ways of quickly object mapping the json data?

        URL myURL = new URL(url);
        /* Open a connection to that URL. */
        URLConnection ucon = myURL.openConnection();
        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        InputStreamReader reader = new InputStreamReader(is);
        long tickCount = System.currentTimeMillis();
        Policy[] policies = new Gson().fromJson(reader, Policy[].class);
        long endCount = System.currentTimeMillis() - tickCount;
        Log.d("Time to pull policies in milliseconds", "" + endCount);
CF_Maintainer
  • 973
  • 2
  • 12
  • 30
  • Please don't use the Android emulator for performance measurement. It's performance model is significantly different from that of a real device. – Jesse Wilson Feb 21 '13 at 03:04

5 Answers5

5

I've seen questions like this come up before, and the general consensus is that Jackson is much faster than Gson. See the following links for more information:

Here is one which specifically discusses Android: http://ubikapps.net/?p=525

Community
  • 1
  • 1
Matt Solnit
  • 32,152
  • 8
  • 53
  • 57
4

Have you tried the mixing the GSON streaming parser with the Gson object? http://sites.google.com/site/gson/streaming (look for the Mixed read example).

This approach may help since Gson reads in an entire parse tree and then acts on it. With a large array list, reading in all elements and attempting to parse may cause lot of memory swaps (or thrashing). This approach will read in one element at a time.

Hope this helps.

Joel
  • 41
  • 1
  • 2
    +1 - the DOM approach on GSON is quick to implement, but really slow to process large responses. I use it for prototypes, and then optimise down to the Streaming method for production. I think more people need to realise GSON is not so slow, its how you use it (not saying it is the fastest, just that many don't use streaming) – Richard Le Mesurier Jun 20 '12 at 09:15
2

You'd probably get better performance if you wrapped that InputStream in a BufferedInputStream with a nice big buffer...

3 minutes is insane. I seldom run the emulator but I have an app with a ~1.1MB JSON asset and that takes around 5 seconds to load and process on hardware.

(Which is still far too long, but still).

Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
1

I've found that I can speed up gson.fromJSON quite considerably by not modelling all the elements in the JSON that I won't need. GSON will happily fill in only what is specified in your response classes.

Nova Entropy
  • 5,727
  • 1
  • 19
  • 32
1

I have found that CREATING a Gson instance is a very expensive operation, both in terms of CPU used and memory allocated.

Since Gson instances are thread-safe, constructing and reusing a single static instance pays off, especially if you are serializing / deserializing often.

velis
  • 8,747
  • 4
  • 44
  • 64