Skip to content

Instantly share code, notes, and snippets.

@junwen12221
Created December 18, 2016 01:10
Show Gist options
  • Save junwen12221/b008836567f05f57fe1a5c858592604c to your computer and use it in GitHub Desktop.
Save junwen12221/b008836567f05f57fe1a5c858592604c to your computer and use it in GitHub Desktop.
map性能测试
package one;
import java.util.*;
/**
* http://baike.xsoftlab.net/view/250.html
* Size:100 * 10000 循环11次 去掉第一次测试数据
*/
public class Test {
static int hashMapW = 0;
static int hashMapR = 0;
static int linkMapW = 0;
static int linkMapR = 0;
static int treeMapW = 0;
static int treeMapR = 0;
static int hashTableW = 0;
static int hashTableR = 0;
public static void main(String[] args) {
for (int i = 0; i < 11; i++) {
Test test = new Test();
test.test(100 * 10000);
System.out.println();
}
System.out.println("hashMapW = " + hashMapW / 10);
System.out.println("hashMapR = " + hashMapR / 10);
System.out.println("linkMapW = " + linkMapW / 10);
System.out.println("linkMapR = " + linkMapR / 10);
System.out.println("treeMapW = " + treeMapW / 10);
System.out.println("treeMapR = " + treeMapR / 10);
System.out.println("hashTableW = " + hashTableW / 10);
System.out.println("hashTableR = " + hashTableR / 10);
}
public void test(int size) {
int index;
Random random = new Random();
String[] key = new String[size];
// HashMap 插入
Map<String, String> map = new HashMap<String, String>();
long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
key[i] = UUID.randomUUID().toString();
map.put(key[i], UUID.randomUUID().toString());
}
long end = System.currentTimeMillis();
if (hashMapW==0)
hashMapW=1;
else
hashMapW += (end - start);
System.out.println("HashMap插入耗时 = " + (end - start) + " ms");
// HashMap 读取
start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
index = random.nextInt(size);
map.get(key[index]);
}
end = System.currentTimeMillis();
if (hashMapR==0)
hashMapR=1;
else
hashMapR += (end - start);
System.out.println("HashMap读取耗时 = " + (end - start) + " ms");
// LinkedHashMap 插入
map = new LinkedHashMap<String, String>();
start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
key[i] = UUID.randomUUID().toString();
map.put(key[i], UUID.randomUUID().toString());
}
end = System.currentTimeMillis();
if (linkMapW==0)
linkMapW=1;
else
linkMapW += (end - start);
System.out.println("LinkedHashMap插入耗时 = " + (end - start) + " ms");
// LinkedHashMap 读取
start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
index = random.nextInt(size);
map.get(key[index]);
}
end = System.currentTimeMillis();
if (linkMapR==0)
linkMapR=1;
else
linkMapR += (end - start);
System.out.println("LinkedHashMap读取耗时 = " + (end - start) + " ms");
// TreeMap 插入
key = new String[size];
map = new TreeMap<String, String>();
start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
key[i] = UUID.randomUUID().toString();
map.put(key[i], UUID.randomUUID().toString());
}
end = System.currentTimeMillis();
if (treeMapW==0)
treeMapW=1;
else
treeMapW += (end - start);
System.out.println("TreeMap插入耗时 = " + (end - start) + " ms");
// TreeMap 读取
start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
index = random.nextInt(size);
map.get(key[index]);
}
end = System.currentTimeMillis();
if (treeMapR==0)
treeMapR=1;
else
treeMapR += (end - start);
System.out.println("TreeMap读取耗时 = " + (end - start) + " ms");
// Hashtable 插入
key = new String[size];
map = new Hashtable<String, String>();
start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
key[i] = UUID.randomUUID().toString();
map.put(key[i], UUID.randomUUID().toString());
}
end = System.currentTimeMillis();
if (hashTableW==0)
hashTableW=1;
else
hashTableW += (end - start);
System.out.println("Hashtable插入耗时 = " + (end - start) + " ms");
// Hashtable 读取
start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
index = random.nextInt(size);
map.get(key[index]);
}
end = System.currentTimeMillis();
if (hashTableR==0)
hashTableR=1;
else
hashTableR += (end - start);
System.out.println("Hashtable读取耗时 = " + (end - start) + " ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment