开发者

Cost of isEqualToString: vs. Numerical comparisons

开发者 https://www.devze.com 2023-02-09 23:30 出处:网络
I开发者_StackOverflow社区\'m working on a project with designing a core data system for searching and cataloguing images and documents.One of the objects in my data model is a \'key word\' object.Ever

I开发者_StackOverflow社区'm working on a project with designing a core data system for searching and cataloguing images and documents. One of the objects in my data model is a 'key word' object. Every time I add a new key word I first want to first run though all of the existing keywords to make sure it doesn't already exist in the current context.

I've read in posts here and in a lot of my reading that doing string comparisons is a far more expensive processing than some other comparison operations. Since I could easily end up having to check many thousands of words before a new addition I'm wondering if it would be worth using some method that would represent the key word strings numerically for the purpose of this process. Possibly breaking down each character in the string into a number formed from the UTF code for each character and then storing that in an ID property for each key word.

I was wondering if anyone else thought any benefit might come from this approach or if anyone else had any better ideas.


What you might useful is a suitable hash function to convert your text strings into (probably) unique numbers. (You might still have to check for collision effects.)

Comparing intrinsic numbers in C code is a much faster for several reasons. It avoids the Objective C runtime dispatch overhead. It requires accessing less total memory. And the executable code for each comparison is usually just an instruction or 3, rather than a loop with incrementers and several decision points.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号