如何解决在Android上的XML解析性能问题 [英] How to solve the XML parsing performance issue on Android

查看:339
本文介绍了如何解决在Android上的XML解析性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要读一个XML文件,在Android上一下〜4000行。首先,我试过的SimpleXML库的,因为它是最简单的,并花了大约2分钟,我的HTC Desire。所以,我想,也许SimpleXML的是因为反射这么慢而这一切的库使用,其他的魔法。我改写我的解析器和使用内置的DOM解析方法与性能的一些特殊的关注。这帮助了一点,但还仍花了约60秒,这仍然是完全不能接受的。经过一番研究,我发现<一href="http://www.developer.com/ws/article.php/10927_3824221_2/Android-XML-Parser-Performance.htm">this文章对developer.com 。有一些图表,显示其他两个可用的方法 - SAX解析器和Android的XML拉解析器 - 同样缓慢。并在文章的最后,你会发现下面的语句:

I have to read a XML file with about ~4000 lines on Android. First I tried the SimpleXML library because it's the easiest and it took about 2 minutes on my HTC Desire. So I thought maybe SimpleXML is so slow because of reflection and all the other magic that this library uses. I rewrote my parser and used the built-in DOM parsing method with some special attention for performance. That helped a bit but it still took about 60 seconds which is still totally unacceptable. After a bit of research I found this article on developer.com. There are some graphs that show that the other two available methods - the SAX parser and Android's XML Pull-Parser - are equally slow. And at the end of the article you'll find the following statement:

第一个惊喜我是在所有三种方法有多慢者。用户   不想等待太久在手机上的效果,所以解析   任何超过几十个记录可能会要求一个不同的方法。

The first surprise I had was at how slow all three methods were. Users don't want to wait long for results on mobile phones, so parsing anything more than a few dozen records may mandate a different method.

这可能是一个不同的方法?怎么做,如果您有多个几十记录?

What might be a "different method"? What to do if you have more than "a few dozen records"?

推荐答案

我只是做了在Android(和其他平台)比较分析器一些PERF的测试。被解析的XML文件只有500行左右(其Twitter搜索Atom提要),但拉动和DOM解析可以通过在三星Galaxy S2或摩托罗拉Xoom2第二约5该等文件翻腾。 SimpleXML的(粉色图中)所使用的最慢的OP关系与DOM解析。

I just did some perf testing comparing parsers on Android (and other platforms). The XML file being parsed is only 500 lines or so (its a Twitter search Atom feed), but Pull and DOM parsing can churn through about 5 such documents a second on a Samsung Galaxy S2 or Motorola Xoom2. SimpleXML (pink in the chart) as used by the OP ties for slowest with DOM parsing.

SAX解析是一个数量的的对我的两个Android设备,管理的40文档/秒单线程的,和65 + /秒多线程的。

SAX Parsing is an order of magnitude faster on both of my Android devices, managing 40 docs/sec single-threaded, and 65+/sec multi-threaded.

对Android的XML解析方法的性能对比

借助 code 是在github上提供和讨论<一个href="http://steveliles.github.com/comparing_java_xml_parsing_mechanisms_for_android.html">here.

The code is available in github, and a discussion here.

这篇关于如何解决在Android上的XML解析性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆