views:

718

answers:

3

Which is better approach between use of NSDictionary and NSObject with NSStrings as properties, in terms of faster performance and efficient memory management, if the code is written for an iPhone application?

E.g. If an application deals with parsing an XML file which is as follows: < ?xml version="1.0" encoding="utf-8" ?>

< FirstName>AAA< / FirstName>

< MiddleName>BBB< / MiddleName>

< LastName>ZZZ< / LastName>

< Age>28< /Age>

< Gender>Male< /Gender>

< /Sync>

then, while storing up the parsed values, which approach is more efficient and proper, if the XML file size can be little bigger? Is storing the parsed values in a 'NSDictionary' against the respective 'keys'(e.g. Key=FirstName Value=AAA) efficient and proper as compared to storing the same parsed data into the NSString variables(i.e. having NSString properties of all the interested nodes to store the respective values. e.g. NSString *firstName will store "AAA") of a 'NSObject' subclass? I'm looking for more faster performance as well as the memory efficiency.

A: 

Not a real answer: You could also use a NSArray with key-value coding, but I don't know about the performance compared with NSObject / NSDictionary.

But I'm pretty sure that using NSObject will be the fastest as there's no overhead at all.

Koraktor
A: 

in terms of faster performance and efficient memory management

The answer probably comes down to your app - how many of these obejcts will you have, how will you access them - are they written more than read etc.

I would give you some general advice - get it working first, then optimise for speed and size if you need it. Do not optimise something before it is written.

Roger Nolan
A: 

In terms of performance, nothing is better than actually perform the test. People have already been surprised with NSArray, which appears to be more complex than expected, just for the sake of performance optimization.

In other terms, I would say that NSDictionary is more flexible than custom NSObject, should new properties be added in future versions of your XML file. In couterpart, I would also say that custom NSObject with explicitely named properties is more readable, thus more maintainable, than NSDictionary.

mouviciel
Yeah, I think you said it right. It mostly depends on the app as well. Well, I, yet, have to test both the results, but I'm more inclined to think that, the flexibility which I'll get(apart from being able to store node names of XML,as keys) by using NSDictionary is edge over the use of NSObject
Code.Warrior