我在互联网上看到应该使用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
}
}
}
我已经多次这样做了 - 外部循环执行了20次每次为k做一次操作,但结果并不好。有时候进行1000万次插入比1百万次还要快,因为我现在使用的不是正确的测量时间方法(System.nanoTime())。
编辑2:是的,我正在使用Sun JVM。
编辑3:我可能在代码中做错了什么,我会看看更改是否能实现我想要的效果。
编辑4:我的错误,似乎System.nanoTime()可以工作。太好了。