title: UNEXPECTED BEHAVIOR OF currentTimeMillis() in JAVA
body: <p>Why does it take more time for insertion in the same SET the first time and much less time when I try to insert the second time. Why there is a change in the behavior for the same code?
  The Employee class contains two fields and generates a hash code. This is of no use here but related to a different program. What I am trying to do is to calculate the complexity of various Collections for better performance on CRUD operations. But when I first add 200000 elements into the HashSet it takes more time. When the same code runs again it takes much lesser time. I don't know how it is working internally. Any help is appreciated.</p>
  <p>IN THE OUTPUT THE HASHMAP CORRESPONDS TO THE HASHSET ONLY. REFER TO THE CODE.</p>
  <pre><code>class Employee {
      String name;
      int age;
      
      @Override
      public int hashCode() {
          return age;
      }
  }
  
  public class NonDuplicateObjects {
      private static final int COLLECTION_SIZE = 2000000;
      private static final Object DUMMY =`enter code here` new Object();
      public static void main(String[] args) {    
          Employee e = new Employee();
          e.age = 0;
          e.name = &quot;Rahim&quot;;
          Employee[] eArr = new Employee[COLLECTION_SIZE];
          ArrayList&lt;Employee&gt; al = new ArrayList&lt;&gt;();
          HashMap&lt;Employee, Object&gt; hm = new HashMap&lt;&gt;(COLLECTION_SIZE);
          HashSet&lt;Employee&gt; hset = new HashSet&lt;&gt;(COLLECTION_SIZE);
          long startTime;
          long timeDiff;
          
          System.out.println(&quot;PROGRAM FOR TIME COMPARISION ON THE BASIS OF CRUD OPERATIONS&quot;);
          System.out.println(&quot;PERFORMING ADDITION/INSERTION:&quot;);
      
          //  Insertion in an array
          startTime = System.currentTimeMillis();
          for (int i=0; i&lt;COLLECTION_SIZE; ++i)
          {
              ++e.age;
              eArr[i] = e;
          }
          timeDiff = System.currentTimeMillis() - startTime;
          System.out.println(&quot;Time taken by array: &quot; + timeDiff);
          System.out.println(&quot;SIZE: &quot; + eArr.length);
          
          //  Insertion in a ArrayList
          e.age = 0;
          startTime = System.currentTimeMillis();
          for (int i=0; i&lt;COLLECTION_SIZE; ++i) {
              ++e.age;
              al.add(e);
          }
          timeDiff = System.currentTimeMillis() - startTime;
          System.out.println(&quot;Time taken by ArrayList: &quot; + timeDiff);
          System.out.println(&quot;SIZE: &quot; + al.size());
      
          //  Insertion in a HashSet
          e.age = 0;
          startTime = System.currentTimeMillis();
          for (int i=0; i&lt;COLLECTION_SIZE; ++i) {
              ++e.age;
              hset.add(e);
          }
          timeDiff = System.currentTimeMillis() - startTime;
          System.out.println(&quot;Time taken by HashMap: &quot; + timeDiff);hset.clear();
          System.out.println(&quot;SIZE: &quot; + hset.size());
          //  Insertion in a HashSet
          e.age = 0;
          startTime = System.currentTimeMillis();
          for (int i=0; i&lt;COLLECTION_SIZE; ++i) {
              ++e.age;
              hset.add(e);
          }
          timeDiff = System.currentTimeMillis() - startTime;
          System.out.println(&quot;Time taken by HashSet: &quot; + timeDiff);
          System.out.println(&quot;SIZE: &quot; + hset.size());
      }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/CXvRa.png" rel="nofollow noreferrer">OUTPUT OF THE PROGRAM. THIRD TIME OUTPUT IS FOR HASHSET ONLY AND NOT HASHMAP. REFER TO THE CODE</a></p>