3

Below is My code:

public class FindTime {

HashSet<String> hashSet = new HashSet<>();

long m1() {
    hashSet.add("hai");
    hashSet.add("me");
    hashSet.add("you ");
    hashSet.add("I");
    hashSet.add("Us");
    Iterator it = hashSet.iterator();
    long startTime = System.currentTimeMillis();
    while (it.hasNext()) {
        System.out.println(it.next());
    }
    return startTime;
}

public static void main(String[] args) {
    FindTime ft = new FindTime();
    long startTime = ft.m1();
    System.out.println("startTime" + startTime);
    long endTime = System.currentTimeMillis();
    System.out.println("End time" + endTime);
    System.out.println("d/W" + (endTime - startTime));
 }
}

I don't know is that one is correct way or not.My requirement is "I want to calculate time taken to Iterate a HashSet".

vels4j
  • 11,208
  • 5
  • 38
  • 63
Prabha
  • 707
  • 2
  • 11
  • 27

3 Answers3

4

To be more precise use System.nanoTime()

public class FindTime {

HashSet<String> hashSet = new HashSet<>();

long m1() 
{
    hashSet.add("hai");
    hashSet.add("me");
    hashSet.add("you ");
    hashSet.add("I");
    hashSet.add("Us");
    Iterator it = hashSet.iterator();
    long startTime = System.nanoTime();
    while (it.hasNext()) {
        System.out.println(it.next());
    }
    return startTime;
}

public static void main(String[] args) {
    FindTime ft = new FindTime();
    long startTime = ft.m1();

    long endTime = System.nanoTime(); //CALCULATE THE END TIME BEFORE PRINTING START TIME
           //BECAUSE PRINT OPERATION WILL ALSO TAKE TIME THAT WILL BE ADDED TO DIFFERENCE

    System.out.println("Start time in nano seconds" + startTime); //No need because you actually need difference

    System.out.println("End time in nano seconds" + endTime);
    System.out.println("Difference in Nano Seconds" + (endTime - startTime));
    //long microsecondsTime = (end - start) / 1000; //If you need in microseconds 
 }
}
Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
  • If I use HashSet,LinkedHasSet and TreeSet with same data with 3 different methods which set will take more time to Iterating. – Prabha Dec 06 '13 at 05:45
  • @Prabha you can test yourself to know which takes more time, but it you look at the code then `LinkedHasSet` extends `HashSet` and will return the same iterator so these both will take almost exact time and `TreeSet` uses `navigableKeySet()` iterator and I don't know whether it takes more time or less. – Deepak Bhatia Dec 06 '13 at 06:19
3

Your code is correct. You've calculated the time which taken for iterating and printing the output in console. Print may take more time than iteration.

You may have also returned the execution time by return System.currentTimeMillis()- startTime;

See Also : Do not use System.out.println in server side code

Community
  • 1
  • 1
vels4j
  • 11,208
  • 5
  • 38
  • 63
  • Than you very much. If I use HashSet,LinkedHasSet and TreeSet with same data which set will take more time to Iterating. – Prabha Dec 06 '13 at 05:39
  • No each one has separate implementation and take different time. But you need to test with huge data otherwise you cant find diff. – vels4j Dec 06 '13 at 05:44
  • same data with 3 different methods. Like first method calculate HsahSet and second method TreeSet and third method calculate LinkedHasSet iteration time.Like that can I find the time difference between set classes. – Prabha Dec 06 '13 at 05:52
1

As you telling you need time to take only for iterate not for insertion then you can use it like

long m1() {
hashSet.add("hai");
hashSet.add("me");
hashSet.add("you ");
hashSet.add("I");
hashSet.add("Us");
Iterator it = hashSet.iterator();
long startTime = System.nanoTime();

while (it.hasNext()) {
    System.out.println(it.next());
}
long endTime = System.nanoTime();
System.out.println("time taken in nano seconds" + endTime-startTime); 
return endTime-startTime;
}

reason is if you take time into your main function then it will also calculate the time for insertion. Yes I am agree that it will not affect too much but we know that is not right thing to do.Even here I am printing so here also it will add the printing time that is not exactly correct.

Pankaj Goyal
  • 950
  • 9
  • 15
  • Thank you very much .I want to find which Set is fast among HashSet,TreeSet and LinkedHashSet. How can I check Is am doing process is correct ? – Prabha Dec 12 '13 at 09:21
  • See every collection is having their own advantage as per requirement. you need to go threw it to check out time complexity http://infotechgems.blogspot.in/2011/11/java-collections-performance-time.html – Pankaj Goyal Dec 12 '13 at 09:33