生成唯一的 Base62 哈希值的最快代码
大家好,我想生成独特的 Base62 哈希值 - 类似于使用 C# 执行的tinyurl 和 bit.ly 操作。这将基于 bigint 类型的自动增量字段 ID(就像大多数这些站点一样),
最小字符数将为 1,最大字符数将为 6...如果您必须编写最快的代码(CPU 使用量最少) c# 对于这个哈希你会怎么写呢?
hey guys i want to generate unique base62 hashes - something similar to what tinyurl and bit.ly do using c#. this would be based on an auto increment field ID of type bigint (like most of these sites)
min chars would be 1 and max chars would be 6... if you had to write the fastest code (least amount of cpu usage) in c# for this hash how would you write it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅我对另一个类似的 Stack Overflow 问题的回答:
数据库 ID 需要一个更小的 GUID 替代方案,但 URL 仍然唯一且随机
我发布了一个名为“ShortCodes”的 C# 类,该类正是您正在寻找的,即根据整数/长数生成一个唯一的baseX(其中X是您喜欢的任何东西!)哈希,并再次转换回来。
实际上,我编写这个小类正是为了模仿 TinyUrl.com 和 Bit.ly 等网站的短代码/哈希生成,以达到我自己的目的。
我不能说这是否是实现这一目标的绝对最快方式,但它也不是很慢! :)
Please see my answer to another Stack Overflow question which is similar, here:
Need a smaller alternative to GUID for DB ID but still unique and random for URL
I posted a C# class called "ShortCodes", that does exactly what you're looking for, i.e. generate a unique baseX (where X is anything you like!) hash based upon a integer/long number, and also to convert back again.
I actually wrote this little class precisely to mimic the short code/hash generation of sites like TinyUrl.com and Bit.ly for my own purposes.
I can't say if this is the absolute fastest way of achieving this, but it's not exactly slow either! :)
Eric Lippert 建议查找表< /a> 在一个类似的早期问题中。他的答案也非常适合您的目的。
Eric Lippert suggested lookup tables in a similar earlier question. His answer is perfect for your purposes as well.