我在互联网上看到我应该使用System.nanoTime(),但这对我来说不起作用 – 它给了我毫秒级精度的时间。我只需要在我的函数执行之前和之后的微秒,以便我知道需要多长时间。我使用的是Windows XP。
基本上,我有这样的代码,例如,在一个java链接列表中,可以执行1百万次到10万次的插入。问题是我无法测量精确度;有时将所有内容插入较小的列表中需要较少的时间。
以下是一个例子:
class test
{
public static void main(String args[])
{
for(int k=1000000; k<=10000000; k+=1000000)
{
System.out.println(k);
LinkedList aux = new LinkedList();
//need something here to see the start time
for(int i=0; i
aux.addFirst(10000);
//need something here to see the end time
//print here the difference between both times
}
}
}
我做了很多次 – 有一个外部循环为每个k做20次,但结果不好。有时候,花费更少的时间来进行1000万次插入超过100万次,因为我没有得到正确的测量时间与我现在使用的(System.nanoTime())
编辑2:是的,我正在使用Sun JVM。
编辑3:我可能在代码中做错了,我会看到如果改变它做我想要的。
编辑4:我的错误,似乎System.nanoTime()工作。唷。