gpt4 book ai didi

java - 为什么执行时间显示为 0 毫秒(java 程序)?

转载 作者:行者123 更新时间:2023-11-29 05:05:44 25 4
gpt4 key购买 nike

I am writing a word count program (very basic) and try to find its execution time . What is strange is that sometime it shows 0 milliseconds after execution of same input for which earlier it gave 15 milliseconds.

Moreover sometime it shows 16 milliseconds and sometime 15 milliseconds for same input.Why is that variance? What could be the possible reason for it?

程序如下:

import java.util.*;


public class WordCount{

public static void main(String [] args){

Scanner scan=new Scanner(System.in);
System.out.println("Enetr the Line of String");
String words=scan.nextLine();
long startTime = System.currentTimeMillis();

wordCount(words);


long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("\n TotalTime = "+totalTime + " mills");

}//main

public static void wordCount(String words){
Integer c=0;
String wor[]=words.split(" ");

//System.out.println(""+Arrays.toString(wor));


HashMap<String,Integer> hm=new HashMap<String,Integer>();
for(String word: wor){
// c=(Integer)hm.get(word);
c=hm.get(word);
c=(c==null)?1:c+1; //no c++
hm.put(word,c);
}

Set keySet=hm.keySet();

Iterator it=keySet.iterator();

while(it.hasNext()){
String key=(String)it.next();

System.out.println(""+key+" = " + hm.get(key));
}


}//wordCount

}//class

Output screen :

H:\Computer Science\Java>java WordCount
Enetr the Line of String
the is an the is an a is an the
the = 3
a = 1
is = 3
an = 3

TotalTime = 15 mills

H:\Computer Science\Java>java WordCount
Enetr the Line of String
the is an the is an a is an the
the = 3
a = 1
is = 3
an = 3

TotalTime = 16 mills

H:\Computer Science\Java>java WordCount
Enetr the Line of String
the is an the is an a is an the
the = 3
a = 1
is = 3
an = 3

TotalTime = 0 mills

H:\Computer Science\Java>java WordCount
Enetr the Line of String
the is an the is an a is an the
the = 3
a = 1
is = 3
an = 3

TotalTime = 0 mills

最佳答案

System.currentTimeMillis()JavaDoc我们读到方法:

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

对于 Windows,此方法的解决时间长于 1 毫秒(在您的情况下约为 16 毫秒),这是更新计时器所需的最短时间。这意味着,当您两次调用 System.currentTimeMillis() 时,您会得到两个完全相同的结果(这说明了 0)或两个结果,它们的差异在于确定操作系统时间所需的时间量.

考虑使用 System.nanoTime() 而不是 System.currentTimeMillis()。它产生更精确的结果。您可以在 the Oracle blog 阅读有关时间测量的更多信息以以下段落结尾:

If you are interested in measuring/calculating elapsed time, then always use System.nanoTime(). On most systems it will give a resolution on the order of microseconds. Be aware though, this call can also take microseconds to execute on some platforms.

关于java - 为什么执行时间显示为 0 毫秒(java 程序)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30423681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com