考虑到sql server以及c#,最多只能用decimal类型,也就是29位的数字,做了下面这个数字型id生成器:
class Program
{
static void Main(string[] args)
{
int i = ;
Timing t = new Timing(); t.Start();
while(i-->)
UniqueIdGenerator.Next();
t.Stop(); t.Display("");
} } public static class UniqueIdGeneratorHelper
{
public static long IP2Long(String strIP)
{
long[] ip = new long[];
string[] s = strIP.Split('.');
ip[] = long.Parse(s[]);
ip[] = long.Parse(s[]);
ip[] = long.Parse(s[]);
ip[] = long.Parse(s[]);
return (ip[] << ) + (ip[] << ) + (ip[] << ) + ip[];
}
} public static class UniqueIdGenerator
{
static UniqueIdGenerator()
{
ip = UniqueIdGeneratorHelper.IP2Long("192.168.1.21");//需要自己从配置文件中读取
} private static long ip=;
public static decimal Next()
{
return decimal.Parse(DateTime.Now.ToString("yyyyMMddHHmmssff") + ip.ToString() + GetSequence().ToString());
} private static int curSeq = ;
private static object o = ;
private static int GetSequence()
{
lock (o)
{
if (curSeq > )
curSeq = ;
return curSeq++;
}
}
}
十万次请求,花了半秒不到,应该还行。