4

I have implemented a simple XML parser using the official Android guide on the XmlPullParser to parse a very simple and short XML file (120 lines, 10.5Kb). On my HTC One X running Android 4.1.1, it takes a fraction of a second to parse it. But on my HTC Hero running Android 2.1, it took more than 3 minutes...

I know the hardware between the 2 is very different, but 3 minutes for such a small file? It's unacceptable... Especially since the XmlPullParser has been available since API 1, it makes no sense to be this slow.

To try and pinpoint the problem, I went through the parsing code step by step. And I noticed that the nextTag() is the one that's taking a very long time to process, everything else seems considerably fast. Dunno if this is the only problem or not...

Any ideas how can I fix this?

rfgamaral
  • 16,546
  • 57
  • 163
  • 275
  • JIT (http://en.wikipedia.org/wiki/Just-in-time_compilation)? 2.1 doesn't have it. – Paul Dec 23 '12 at 20:07
  • @Paul I'm not sure that's it... I've also tested this one two other devices, both ZTE Blade. One running the stock Android 2.2 and the other running CyanogenMOD with Android 2.3.x. They both have JIT enabled and the feed still take ages to parse... – rfgamaral Dec 24 '12 at 02:13

1 Answers1

4

After a more thorough debugging and researching I realized that the problem was not on the XmlPullParser as I suspected, it just didn't make any sense...

The real problem was on the fact that I was parsing a date and using SimpleDateFormat, specifying a different locale than the one currently in use. Android versions below ICS (if I'm not mistaken) have serious bugs with this and takes ages to load the needed locale information for SimpleDateFormat. Those versions loads and caches the default system locale (user setting) and Locale.US on a system boot and if any of those locales are used with SimpleDateFormat than the operation is fast. Otherwise, it's slow as hell.

I was also creating a new instance of SimpleDateFormat for each date parsing, which was unnecessary and stupid. Using a single object as an instance variable, decreased the time it took to parse the file considerably.

rfgamaral
  • 16,546
  • 57
  • 163
  • 275