我是靠谱客的博主 等待丝袜,这篇文章主要介绍java-生成唯一的序列id,UUID.randomUUID().toString();,现在分享给大家,希望可以做个参考。

public class TestSeq {
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");

	public static void main(String[] args) throws UnsupportedEncodingException, ParseException {
		
		long cost = 0;
		for (int i = 0; i < 100000; i++) {
            long start = System.currentTimeMillis();
            String teString="11111111111111-"+dateFormat.format(new Date());
            long end = System.currentTimeMillis();
            cost += (end - start);
        }
		System.out.println("Differ:[" + cost + "]");

		for (int i = 0; i < 100000; i++) {
            long start = System.currentTimeMillis();
            String teString=String.valueOf(Math.random()).replaceAll("0.", "")+"-11111111111111-"+dateFormat.format(new Date());
            long end = System.currentTimeMillis();
            cost += (end - start);
        }
		System.out.println("Differ:[" + cost + "]");

		for (int i = 0; i < 100000; i++) {
            long start = System.currentTimeMillis();
            String teString=String.valueOf(Math.random()).replaceAll("0.", "");
            long end = System.currentTimeMillis();
            cost += (end - start);
        }
        System.out.println("Differ:[" + cost + "]");

        for (int i = 0; i < 100000; i++) {
            long start = System.currentTimeMillis();
            double d = Math.random();
            long end = System.currentTimeMillis();
            cost += (end - start);
        }
        System.out.println("Differ:[" + cost + "]");
		

		for (int i = 0; i < 100000; i++) {
            long start = System.currentTimeMillis();
            String d = UUID.randomUUID().toString();
            long end = System.currentTimeMillis();
            cost += (end - start);
        }
        System.out.println("Differ:[" + cost + "]");
		
		
	}
}
Differ:[297]
Differ:[734]
Differ:[843]
Differ:[843]
Differ:[1561]

每一次运行的结果都不太一样,如果使用的话还是建议使用UUID.randomUUID().toString(),毕竟保证唯一是必须的。

最后

以上就是等待丝袜最近收集整理的关于java-生成唯一的序列id,UUID.randomUUID().toString();的全部内容,更多相关java-生成唯一内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部