Re: iPhone: validate a NSString for US zipcode
Re: iPhone: validate a NSString for US zipcode
- Subject: Re: iPhone: validate a NSString for US zipcode
- From: David Duncan <email@hidden>
- Date: Thu, 7 Jan 2010 13:44:24 -0800
On Jan 7, 2010, at 10:00 AM, Paul Bruneau wrote:
> To help make this thread more Cocoa-y, I would like to ask: Do the NSSet and NSArray methods like -containsObject perform in a fashion comparable to a home-rolled binary search? I greatly prefer to use the Cocoa stuff rather than try to remember/learn how to properly code such things.
Since NSArray is unordered I would not expect its containsObject to do better than O(n). If NSSet is an ordered container, it should be able to do O(lg n). I don't believe we give any guarantees as to performance of these methods however, but a simple implementation of either data structure would likely give you these performance characteristics.
> Plus, the theory of premature-optimazation-is-bad would say just use the Cocoa stuff and only if performance is proven to be a problem, then look at optimizing, wouldn't it?
The theory of premature optimization would say that implementing an NSArray clone is a waste of time when you have an NSArray implementation in hand and no evidence that your clone would grant a performance improvement.
But premature optimization is not a reason to avoid improving an algorithm by using NSSet instead of NSArray when the algorithm is clearly designed to be implemented with a set. That said, there may be many other reasons not to do so, such as being able to show that such a change would cause worse performance problems elsewhere, or showing that such a change has a poor cost:benefit ratio.
In terms of the original question, validating the ZIP code before submitting to the service could be a form of premature optimization. Since the service has to validate the ZIP code in order to do real work, unless you could prove that doing so was a performance win, you should not do it in the name of performance. But there may be other reasons to do so, although from some of the other responses to this thread, it would seem that there are just as many other reasons not to :).
--
David Duncan
Apple DTS Animation and Printing
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden