首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

由UUID跟短域名想到的(续一)

2012-11-10 
由UUID和短域名想到的(续一)由UUID和短域名想到的,帖子http://www.iteye.com/topic/1017979由UUID和短域名

由UUID和短域名想到的(续一)
由UUID和短域名想到的,帖子http://www.iteye.com/topic/1017979
由UUID和短域名想到的,博客http://zhaiyz.iteye.com/blog/1018906

本文继续研究22位UUID的使用。
现在用来生成22位UUID的方法使用william_ai这位仁兄提供的。

现对产生22位UUID的效率与产生36位UUID的效率做对比。
以后还会继续对分别用它们做主键,建立索引的使用效率做对比。

代码如下:

import java.util.UUID;public class UUIDUtil {public static final char[] charMap;static {charMap = new char[64];for (int i = 0; i < 10; i++) {charMap[i] = (char) ('0' + i);}for (int i = 10; i < 36; i++) {charMap[i] = (char) ('a' + i - 10);}for (int i = 36; i < 62; i++) {charMap[i] = (char) ('A' + i - 36);}charMap[62] = '_';charMap[63] = '-';}public static String hexTo64(String hex) {StringBuffer r = new StringBuffer();int index = 0;int[] buff = new int[3];int l = hex.length();for (int i = 0; i < l; i++) {index = i % 3;buff[index] = Integer.parseInt("" + hex.charAt(i), 16);if (index == 2) {r.append(charMap[buff[0] << 2 | buff[1] >>> 2]);r.append(charMap[(buff[1] & 3) << 4 | buff[2]]);}}return r.toString();}public static String getUUID() {StringBuffer sb = new StringBuffer("0");String uuid = UUID.randomUUID().toString();uuid = uuid.replaceAll("-", "").toUpperCase();sb.append(uuid);uuid = hexTo64(sb.toString());return uuid;}public static void main(String[] args) {int count = 100000;// 计算循环一千万次所用时间long start = System.nanoTime();for (int i = 0; i < count; i++) {}long end = System.nanoTime();long f = end - start;System.out.println("循环10W次所用时间:" + Float.valueOf(f) / 1000000 + "毫秒");@SuppressWarnings("unused")String uuid = null;// 计算使用getUUID()得到一千万个数据所用时间start = System.nanoTime();for (int i = 0; i < count; i++)uuid = getUUID();end = System.nanoTime();long l1 = end - start - f;System.out.println("计算使用getUUID()得到10W个数据所用时间:"+ (Float.valueOf(end - start - f) / 1000000) + "毫秒");// 计算使用randomUUID()得到一千万个数据所用时间start = System.nanoTime();for (int i = 0; i < count; i++)uuid = UUID.randomUUID().toString();end = System.nanoTime();long l2 = end - start - f;System.out.println("计算使用randomUUID()得到10W个数据所用时间:"+ (Float.valueOf(end - start - f) / 1000000) + "毫秒");System.out.println("用getUUID()取得一个数据所用的时间是用randomUUID()取得一个数据的"+ (Float.valueOf(l1) / Float.valueOf(l2)) + "倍");}}

输出结果:
循环10W次所用时间:1.238204毫秒计算使用getUUID()得到10W个数据所用时间:1324.7645毫秒计算使用randomUUID()得到10W个数据所用时间:283.0976毫秒用getUUID()取得一个数据所用的时间是用randomUUID()取得一个数据的4.679533倍


结论:
虽然产生22位UUID所用的时间是产生36位UUID的时间的4倍多,但产生10W个数据所用的时间为1秒多,也是可以接受的,而且本人机子的性能为中等,服务器上应该快的多,完全够用。

热点排行