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)