Reputation: 2229
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 gave15 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?
Here is the program:
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
Upvotes: 1
Views: 2626
Reputation: 14401
In the System.currentTimeMillis()
JavaDoc we read that the method:
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.
For Windows the resolution time for this method is longer than 1ms (in your case around 16ms) and this is a minimum time needed to update the timer. What this mean is that when you call System.currentTimeMillis()
twice you get either two exactly same results (that explains 0) or two results which differ by amount of time needed to resolute time on your operating system.
Consider using System.nanoTime()
instead System.currentTimeMillis()
. It produces more precise results. You can read more about time measurement at the Oracle blog which concludes with the following paragraph:
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.
Upvotes: 2
Reputation: 1565
Execution time of your program is computationally 0. Counting words in a small sentence does not take time in milliseconds range. You are sometimes getting 15ms or 16ms most probably because of I/O (System.out), operating systems scheduling delays, other programs working on your system, or limitations of System.currentTimeInMillis()
implementation. Actual working time of your word count program should be 0, take out the I/O.
Upvotes: 1