gpt4 book ai didi

android - 如何解决 Android 上的 XML 解析性能问题

转载 作者:IT老高 更新时间:2023-10-28 23:29:33 27 4
gpt4 key购买 nike

我必须在 Android 上读取大约 4000 行的 XML 文件。首先我尝试了SimpleXML library因为这是最简单的,在我的 HTC Desire 上花了大约 2 分钟。所以我想可能是 SimpleXML 太慢了,因为反射和这个库使用的所有其他魔法。我重写了我的解析器并使用了内置的 DOM 解析方法,并特别注意了性能。这有点帮助,但仍然需要大约 60 秒,这仍然是完全 Not Acceptable 。经过一番研究,我发现this article on developer.com .有一些图表显示其他两种可用的方法——SAX 解析器和 Android 的 XML Pull-Parser——同样慢。在文章的最后你会发现如下声明:

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.

什么是“不同的方法”?如果您有超过“几十条记录”怎么办?

最佳答案

原始答案,2012 年

(注意:请务必阅读下面的 2016 年更新!)

我刚刚做了一些性能测试,比较了 Android(和其他平台)上的解析器。被解析的 XML 文件只有 500 行左右(它是一个 Twitter 搜索 Atom 提要),但在三星 Galaxy S2 或摩托罗拉 Xoom2 上,Pull 和 DOM 解析每秒可以处理大约 5 个这样的文档。 OP 使用的 SimpleXML(图表中的粉红色)用于 DOM 解析最慢。

SAX 解析在我的两台 Android 设备上都快了一个数量级,管理 40 文档/秒的单线程和 65+/秒的多线程。

Android 2.3.4:

performance comparison of xml parsing methods on Android

code可以在 github 上找到,讨论 here .

2016 年 3 月 18 日更新

好的,已经快 4 年了,世界已经在前进。我终于有时间重新运行测试:

  1. 运行 Android 4.1.2 的三星 Galaxy S3
  2. 运行 Android 4.4.4 的 Nexus7 (2012)
  3. 运行 Android 6.0.1 的 Nexus5

在 Android 4.4.4 和 Android 6.0.1 之间,情况发生了翻天覆地的变化,我们有了一个新的赢家:以超过 SAX 两倍的吞吐量提取解析 FTW。不幸的是,我不知道此更改的确切时间,因为我没有任何运行 Android > 4.4.4 和 < 6.0.1 的设备。

Android 4.1.2:

performance comparison of xml parsing methods on Android 4.1.2

Android 4.4.4:

performance comparison of xml parsing methods on Android 4.4.4

Android 6.0.1:

performance comparison of xml parsing methods on Android 6.0.1

关于android - 如何解决 Android 上的 XML 解析性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7224318/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com