【发布时间】:2016-05-14 18:56:51
【问题描述】:
我试图在不同类型的集合上测量不同操作的时间并想比较它们,但我得到的值在相同类型的集合上差别很大,比如因子 1000。我使用我常用的技术在这里阅读:How do I time a method's execution in Java?
我比较了 Hashset、TreeSet 和 LinkedHashSet。 我用 1 000 000 个整数填充集合,使用方法 contains() 并遍历集合。我测量了每次操作的时间,结果差异很大。所以我用相同类型的新集合做了第二次,我得到的执行时间似乎不合法。
同一类型的集合需要一次 1400 毫秒然后 300 毫秒才能被填充。这是为什么呢?
这是一个代码示例,它可能会让我的意思更清楚:
public static void main(String[] args){
HashSet<Integer> firstHashSet = new HashSet<>(predefinedSize);
HashSet<Integer> secondHashSet = new HashSet<>(predefinedSize);
LinkedHashSet<Integer> firstLinkedHashSet = new LinkedHashSet<>(predefinedSize);
LinkedHashSet<Integer> secondLinkedHashSet = new LinkedHashSet<>(predefinedSize);
TreeSet<Integer> firstTreeSet = new TreeSet<>();
TreeSet<Integer> secondTreeSet = new TreeSet<>();
int x = 9432;
System.out.println("filling hashSet: <" + fillSet(firstHashSet) + "> milliSeconds");
System.out.println("filling linkedSet: <" + fillSet(firstLinkedHashSet) + "> milliSeconds");
System.out.println("filling treeSet: <" + fillSet(firstTreeSet) + "> milliSeconds");
System.out.println("-------------------------------------------------------------");
System.out.println("filling hashSet: <" + fillSet(secondHashSet) + "> milliSeconds");
System.out.println("filling linkedSet: <" + fillSet(secondLinkedHashSet) + "> milliSeconds");
System.out.println("filling treeSet: <" + fillSet(secondTreeSet) + "> milliSeconds");
这是我的一个填充集的样子:
private static int size = 1000000;
private static int predefinedSize = 2000000;
public static double fillSet(LinkedHashSet<Integer> myHashSet){
double timeStart = System.nanoTime();
for(int i=0; i<size; i++){
myHashSet.add(i);
}
double time = (System.nanoTime() - timeStart)/ Math.pow(10, 6);
return time;
}
输出如下:
filling hashSet: <52.14022> milliSeconds
filling linkedSet: <95.599435> milliSeconds
filling treeSet: <2172.773956> milliSeconds
-------------------------------------------------------------
filling hashSet: <59.096929> milliSeconds
filling linkedSet: <1006.638126> milliSeconds
filling treeSet: <241.36395> milliSeconds
你看到输出差异很大,我认为这取决于我的电脑的计算能力,但我没有在后台运行任何其他程序。 有人可以给我一个解释和/或解决方案吗?
【问题讨论】:
-
也许垃圾收集器在填充数据时正在运行,所以时间不同。
-
现在打开一些其他应用程序,如 chrome、explorer、photoshop 或任何游戏,然后检查基准,你就会知道哪里出了问题
-
确保您打开繁重的任务,然后在打开相同应用的情况下对低处理器和高处理器进行相同的检查
-
那么你就会得到答案
-
有太多方法可以做出错误的基准测试。使用这个:openjdk.java.net/projects/code-tools/jmh