iphone - NSString to int via Hash? for CoreData's performance
Can anyone tell me a way to Hash a NSString to an Integer and this Hash should be identical pairing with the NSString all the time. For a NSString, the int hash value shouldn't change on any condition.
I thought to use [NSString hash], but this post ( Is [NSString hash] computed every time? ) said [NSString hash] will change which means one identical NSString may have different hash value.
This is not what I want.
I explain as follows about the purpose I wish to have such a NSString to Integer hash function.
I am using CoreData to store all my data. Each time I fetch, I fetch for all items whose "CategoryUrl" matches a certain one. The "CategoryUr开发者_开发问答l" is quite long just like a normal long http url but with extra characters inside to show more meta info.
Anyway, for each such a fetch, it is quite slow because I think CoreData will have to compare each item's "CategoryUrl" property with the one that needs to be found. And comparing NSString in CoreData is slow, slower than Integer, right?
My thinking is to hash the "CategoryUrl" for each item before I save it, and store the hash value in a new property for each item. So for every fetch, I just let CoreData compare the hash value of "CategoryUrl", instead of the NSString value. That may boosts the speed.
I did some experiments with fake hash value, it does boost quite much. But I am still not sure because I haven't found a decent real NSString to Integer hash function yet.
Anyone can tell me whether I am thinking in the right direction for the performance of CoreData and what is the best way to hash NSString to Integer?
Thanks
There are documented problems with the NSString built-in hash method, so if you do not want to rely on it you can use the MD5 hash instead. Instead of spamming SO I will just link you back to a previous question with some excellent answers. Note that you will take a slight performance hit when storing data, but your read's should speed up quite a bit.
精彩评论