SAX解析器需要花费大量时间来解析
我创建了一个应用程序,在其中使用 SAX 解析器解析来自服务器的数据。我关注了 此链接
它工作正常,但花了很多时间时间。我需要减少解析内容所花费的时间。 有什么专业提示吗?
I have created an application where I parse data from server using SAX parser. I followed this link
It works fine but it took a lot of time. I need to reduce time taken to parse stuff.
Any pro-tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显而易见的提示是:分析您的代码并确定时间都花在哪里。也许您正在做大量处理某些 SAX 事件的工作。也许时间花在了一些与解析完全无关的事情上。在您进行个人资料之前您无法判断。
The obvious tip is: profile your code and determine where the time is going. Perhaps you are doing a lot of work handling some of the SAX events. Perhaps the time is being spent doing something entirely unrelated to the parsing. You can't tell until you profile.
正如 Ted 所说,分析您的代码。
您确定它是解析时间而不是加载时间吗?您使用的是 wifi 还是移动网络?其他应用程序加载数据的速度有多快?
也就是说,不要使用那么多 ArrayList 对象,具有自定义容器类型的单个数组列表(即 Ticket 对象有价格和日期,不需要有价格数组和日期数组) 。
检查内存使用情况,通常如果速度很慢,就会产生大量垃圾。 (在日志中查找 GC)
对于不会更改的字符串参数使用
final
关键字。所以用setString(final String s)
而不是setString(String s)
。这应该可以防止字符串作为参数传递时出现重复。如果可以的话,使用 JSON 而不是 XML,它更轻量。
进行任何重大更改后,请再次分析您的代码
Like Ted said, profile your code.
Are you sure its the parse time and not the load time? Are you on wifi or a mobile network? How quickly are other apps loading their data?
That said, don't use that many
ArrayList
objects, a single array list with a custom container type (i.e. a Ticket object has a price and date, no need to have a price array and date array).Check you memory usage, usually if things are slow you are generating a ton of garbage. (look for the GC in the log)
Use the
final
keyword for String parameters that won't change. SosetString(final String s)
instead ofsetString(String s)
. This should prevent the Strings from being duplicated when passed as parameters.Use JSON instead of XML if you can, its more light weight.
After you've made any significant change, profile your code again