views:

469

answers:

3

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?

A: 

Eric Lippert suggested lookup tables in a similar earlier question. His answer is perfect for your purposes as well.

Joren
+2  A: 

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! :)

CraigTP
A: 

Care to explain why the "need for speed"?

I can't imagine how any kind of implementation won't be able to generate millions of these in a fraction of a second...

jcinacio