1

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 "CategoryUrl" 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

Community
  • 1
  • 1
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
  • Jackson, hashes by definition, are one-way. I assume you will be needing to get the value of your CategoryURL back at some point? You might have to look at other ways of optimizing your product performance. Please read here for more info on hashing: http://stackoverflow.com/questions/1330612/two-way-keyed-encryption-hash-algorithm. – Perception Jun 28 '11 at 10:37
  • Do you have an index defined for your Core Data 'table'? – Perception Jun 28 '11 at 10:39
  • Yes, I know its one way, but no problem as I will store the real CategoryURL as another property of a item. I use the hash sololy for the speed of fetch. – Jackson Tale Jun 28 '11 at 10:40
  • yes, I have index defined and the NSString "CategoryURL" is indexed. I chose it in xcode – Jackson Tale Jun 28 '11 at 10:40
  • Okay, if you can access the CategoryURL separately then I will recommend you use the MD5 hash in your scenario. – Perception Jun 28 '11 at 10:51
  • Can you show us how you are structuring your fetch in code? – sosborn Jun 28 '11 at 11:14

1 Answers1

0

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.

Community
  • 1
  • 1
Perception
  • 79,279
  • 19
  • 185
  • 195
  • But MD5 hash value is also a string,right? what I need is a Integer – Jackson Tale Jun 28 '11 at 10:56
  • The MD5 hash value will be a shorter string that indexes better and should boost your read speed. If you need more optimization you can convert it to an integer using the FNV hash function described here - http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1. – Perception Jun 28 '11 at 11:05