iphone - NSString 通过哈希转换为 int? CoreData 的性能

发布于 2024-11-17 06:59:01 字数 914 浏览 2 评论 0原文

谁能告诉我一种将 NSString 哈希为整数的方法,并且这个哈希应该始终与 NSString 相同的配对。对于 NSString,int 哈希值在任何情况下都不应该改变。

我想使用 [NSString hash],但是这篇文章( [NSString hash] 计算每个时间?)说[NSString hash]会改变,这意味着一个相同的NSString可能有不同的哈希值。

这不是我想要的。

我解释如下我希望有这样一个 NSString 到 Integer 哈希函数的目的。

我正在使用 CoreData 来存储我的所有数据。每次获取时,我都会获取“CategoryUrl”与某个特定项匹配的所有项目。 “CategoryUrl”非常长,就像正常的长 http url 一样,但内部有额外的字符来显示更多元信息。

无论如何,对于每次这样的获取,它都非常慢,因为我认为 CoreData 必须将每个项目的“CategoryUrl”属性与需要找到的属性进行比较。而且在CoreData中比较NSString很慢,比Integer还慢,对吧?

我的想法是在保存每个项目之前对其“CategoryUrl”进行哈希处理,并将哈希值存储在每个项目的新属性中。因此,对于每次获取,我只是让 CoreData 比较“CategoryUrl”的哈希值,而不是 NSString 值。这可能会提高速度。

我用假哈希值做了一些实验,它确实提升了很多。但我仍然不确定,因为我还没有找到一个像样的真正的 NSString 到 Integer 哈希函数。

任何人都可以告诉我,对于 CoreData 的性能,我的思考方向是否正确,以及将 NSString 哈希为 Integer 的最佳方法是什么?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就是爱搞怪 2024-11-24 06:59:01

NSString 内置哈希方法有记录的问题,因此如果您不想依赖它,可以使用 MD5 哈希代替。我不会发送垃圾邮件,而是将您链接回上一个问题 有一些很好的答案。请注意,存储数据时性能会受到轻微影响,但读取速度应该会加快很多。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文