Better performance with libxml2 or NSXMLParser on the iPhone? Better performance with libxml2 or NSXMLParser on the iPhone? xml xml

Better performance with libxml2 or NSXMLParser on the iPhone?


You could always take a look at my replacement for NSXMLParser. It reads the XML data from a stream rather than keeping it all in memory, and passes it 1KB at a time into libxml (where NSXMLParser hands in everything in one go).

Source code is available on github and my writeup on the memory aspects are on my blog.


I've generally found for larger chunks of data (like the apple example you reference) libxml2 tends to be faster. For smaller chunks of data, the difference is negligible. One advantage i like about NSXMLParser is that it is an Objective-C based implementation of an XML Parser where libxml2 is C based.


libxml2 will always be faster than NSXMLParser for many reasons, however, it is up to you which is more useful to your project.

NSXMLParser, overall, is prettier. The code makes sense, as sax parser's are supposed to be, and it is a real Cocoa class with all the conventions there. If convenience and clean code are your top priorities, then you should stick with NSXMLParser.

While NSXMLParser uses libxml2 on the backend, it is slower due to the foundations of Objective-C and the achilles heel of Objective-C. When parsing XML, you're essentially just doing a bunch of tight loops over and over again while searching for the tags you're interested in.

Herein lies the lesson - when in a tight loop in Objective C that you are unable to utilize Fast Object Enumeration, you are looking at a serious performance hit. Dispatch / Delegate respondsToSelector / and other Objective C base language constructs give you a real disadvantage here.

I'm not going to go into dispatch, but the crux is, whenever you access something like this: "[zomg lolz]" you're handing off a method signature to the objective-c dispatcher to find the target C-function for your Objective-C method signature. This lookup process, when done over and over again, can dramatically reduce your performance.

If you're on an iPhone, go libxml2 and don't look back - but if your target machine has two processors and more ram than god, I'd go NSXMLParser for easier to maintain code.