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?
After a more thorough debugging and researching I realized that the problem was not on the
XmlPullParseras 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 forSimpleDateFormat. Those versions loads and caches the default system locale (user setting) andLocale.USon a system boot and if any of those locales are used withSimpleDateFormatthan the operation is fast. Otherwise, it’s slow as hell.I was also creating a new instance of
SimpleDateFormatfor 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.