📄 testsetperformance.java
字号:
import java.util.*;
public class TestSetPerformance {
private static int REPTEXEC = 10000;
private static int QUANTITY = 100;
public static void main(String[] args) {
System.out.println(REPTEXEC + " repetitions");
testSet(new TreeSet());
testSet(new HashSet());
testSet(new LinkedHashSet());
} // main方法结束
public static void fill(Collection c, int count) {
for(int i = 0; i < count; i++)
c.add(Integer.toString(i));
} // fill方法结束
public static void testSet(Set a) {
System.out.println("_____________________________________");
System.out.println("测试" + a.getClass().getName().replaceAll("\\w+\\.", ""));
fill(a, QUANTITY);
// 测试size()
long start = System.currentTimeMillis();
for (int i = 0; i < REPTEXEC; i++) {
for (int j = 0; j < QUANTITY; j++)
a.size();
}
long end = System.currentTimeMillis();
System.out.println("取大小操作用时: " + (end - start));
// 测试迭代操作
start = System.currentTimeMillis();
for (int i = 0; i < REPTEXEC; i++) {
Iterator it = a.iterator();
while (it.hasNext())
it.next();
}
end = System.currentTimeMillis();
System.out.println("迭代操作用时: " + (end - start));
// 测试包含操作
for(int i = 0; i < REPTEXEC; i++)
a.contains(Integer.toString(i));
end = System.currentTimeMillis();
System.out.println("包含操作用时: " + (end - start));
// 测试增加操作
String s = "test";
start = System.currentTimeMillis();
for (int j = 0; j < REPTEXEC * 10; j++)
a.add(s);
end = System.currentTimeMillis();
System.out.println("增加操作用时: " + (end - start));
} // testSet方法结束
} // TestSetPerformance方法结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -