XMLPerformance
===========================================================================
ABSTRACT
This sample explores two approaches to parsing XML, focusing on performance with respect to speed, memory footprint, and user experience. The XML data used is the current "Top 300" songs from the iTunes store. The data itself is not particularly important to the sample - it was chosen because of its simplicity, availability, and because the size (approximately 850KB) is sufficient to demonstrate the performance issues central to the sample.
===========================================================================
DETAILED DESCRIPTION
The iPhone SDK provides two APIs for parsing XML. At the Objective C level, NSXMLParser implements an event-driven approach with a delegate object implementing methods for handling each of the "events" the parser encounters during its single pass over the XML data. Events most commonly of interest are the beginning and ending of elements and character data within elements. The other API in the SDK, the C library "libxml2", has a similar approach known as SAX ("Simple API for XML"). Because it is C, callback functions are used instead of delegate methods, and the parameters are C strings instead of NSString objects.
This sample allows the user to choose between these two approaches for parsing a simple RSS feed. The feed, iTunes' "Top 300" songs, is parsed into an array of "Song" objects displayed in a table. Details about a song can be viewed by selecting the song. The sample also tracks statistics related to the parse: the amount of time required to download the data, and the amount of time spent parsing the data. These statistics are stored in a SQLite database in the application's Documents directory, and the average (mean) of all runs with each parser can be viewed in a table.
===========================================================================
PERFORMANCE
The focus of this sample is performance. There are three areas of concern: speed, memory, and user experience. For applications dealing with small amounts of XML data, none of these may be significant. In this case, developers should use the API with which they are most comfortable. For most developers, this will be the NSXMLParser API in the Foundation framework.
For large datasets, developers should test their application with an iPhone or iPod touch and evaluate the application's performance with respect to speed, memory, and user experience. It's important that this be done with a device and not with the iPhone Simulator because the Simulator does not accurately reflect the memory and processor constraints. The techniques implemented in this sample can help the developer improve performance and memory usage, if it's determined that such a need exists.
Memory:
NSXMLParser be used with either a NSURL or a NSData. In both cases, all of the XML data is loaded into memory. On iPhone OS, this can be a very significant consideration. The actual parsing will require additional memory, particularly with intermediate objects created and autoreleased. You can create and destroy additional autorelease pools in the course of parsing, and mitigate this somewhat, but you are still managing the memory pool on top of the initial allocation for the XML data itself.
With libxml, you can parse XML data in chunks. This alleviates the need to have all of the data in memory at one time, possibly resulting in a considerably smaller memory footprint. This could be applied to data downloaded using NSURLConnection. The NSURLConnection delegate method connection:didReceiveData: may be called multiple times during a download, and rather than accumulate the data, it can be immediately passed to the libxml parser. When the parser is finished, the data can be discarded. In addition, libxml callbacks use C strings rather than Objective C objects. In general, the overhead for objects is not significant, but in large numbers, in tight loops, this adds up. In particular, when the character data in an XML element is parsed, that data is delivered as one or more parse "events". For NSXMLParser, these events result as the delegate method parser:foundCharacters:, with an autoreleased NSString as the container for the character data. In libxml, the events call in the registered callback function, passing a pointer to a C string buffer. This offers another opportunity to optimize on memory management. Rather than creating an object with each call of the function, the character data can be accumulated in a separate buffer, until all data for the current XML element has been handled. Only at that point does a NSString object need to be created.
Speed:
Speed is obviously important to users, as waiting for long operations to complete is not a good experience. In addition, speed is an indirect reflection of processor load, which is in turn tied to power consumption. Hence, an more rapidly executing code path not only provides a better user experience with the application itself, it consumes less power, leading to longer battery life and a better experience for the overall device.
User Experience:
At times CPU and/or IO intensive operations cannot be avoided. A positive user experience can still be provided in these cases by offloading work to separate threads, using NSThread, NSOperation, or lower level threading APIs such as POSIX threads(pthreads). This sample involves both lengthy IO (downloading the XML data) and CPU intensive work (parsing the XML). To keep the interface responsive, this work is done in a secondary thread. Periodically, the secondary thread updates the primary thread with the results of the work it has done. In turn, the user interface displays the new data to the user. If this approach were not used, the user would experience 3 or more seconds in which the application would appear to hang while the data was being downloaded and parsed.
Metrics:
This sample includes some screenshots and a sample trace document from Instruments. The first screenshot, "StatisticsScreenshot.png", is captured from the application, showing the statistics that might be seen after running each parser several times. The second, "ObjectAllocScreenshot.png", shows the Instruments window with one run from each parser. The top run is a run with the LibXMLParser. Note the significantly smaller peak memory usage. In addition, the trace document itself, included as a zipped archive, can be opened and examined.
===========================================================================
SYSTEM REQUIREMENTS
iPhone OS 2.0
===========================================================================
PACKAGING LIST
AppDelegate
Adds the main UITabBarController's view to the application's window.
ParserChoiceViewController
Provides an interface for choosing and running one of the two available parsers.
SongsViewController
Creates and runs an instance of the parser type chosen by the user, and displays the parsed songs in a table. Selecting a row in the table navigates to a detail view for that song.
DetailViewController
Displays details of a single parsed song.
LabeledCell
Used in the DetailViewController's table, this cell type has a right justified "label" and a left justified "value".
Song
Contains the parsed information about a song.
iTunesRSSParser
Base class for the two parsers, this class handles interactions with a delegate object (the SongsViewController in this sample) and provides basic functionality common to both parsers.
LibXMLParser
Subclass of iTunesRSSParser that uses libxml2 for parsing the XML data.
CocoaXMLParser
Subclass of iTunesRSSParser that uses the Foundation framework's NSXMLParser for parsing the XML data.
StatsViewController
Displays statistics about each parser, including its average time to download the XML data, parse it, and the total average time from beginning the download to completing the parse.
Statistics
Collection of C functions for database
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
官方解析XML示例-XMLPerformance.zip (39个子文件)
XMLPerformance
XMLPerformance.xcodeproj
yile.perspectivev3 46KB
project.pbxproj 22KB
yile.pbxuser 32KB
en.lproj
DetailView.xib 8KB
Localizable.strings 1KB
Stat.png 4KB
MainWindow.xib 21KB
Default.png 15KB
SongsView.xib 10KB
StatsView.xib 10KB
XML.png 4KB
stats.sqlite 2KB
ReadMe.txt 8KB
main.m 3KB
Info.plist 897B
.DS_Store 6KB
Classes
DetailController.m 5KB
AppDelegate.h 3KB
LabeledCell.h 3KB
LibXMLParser.m 17KB
StatsViewController.m 6KB
CocoaXMLParser.h 3KB
LabeledCell.m 4KB
SongsViewController.h 3KB
Statistics.m 14KB
LibXMLParser.h 4KB
SongsViewController.m 7KB
CocoaXMLParser.m 7KB
Statistics.h 4KB
ParserChoiceViewController.h 3KB
DetailController.h 3KB
Song.h 3KB
Song.m 3KB
StatsViewController.h 3KB
AppDelegate.m 3KB
iTunesRSSParser.h 5KB
iTunesRSSParser.m 7KB
ParserChoiceViewController.m 5KB
Prefix.pch 191B
共 39 条
- 1
资源评论
- zouchinasoft2012-04-21代码很全的,收藏了
- yxxxxy2011-09-24我以为是解释代码呢,结果就是官网上的xmlperformance代码~没什么大用,因为官网和文档中都可以免费看到~
- hanbingquan2222011-11-25编译不了,照着网上说的改了一些参数,但是还是找不到一些头文件
yishui0521
- 粉丝: 0
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功