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