0

I know time taken by get() operation in ArrayList is O(1), however I have noticed that first element always taking either more or equal time compare to others. I tried 100's of time. I know it does not matter much as the difference is in nano seconds, but would like to know if there is any logical explanation or it's just Random.

Here' is my program:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ListTest {
    public static void main(String[] args) throws IOException {

        for (int i = 1; i <= 20; i++) {
            System.out.println("============== Iteration ======= "+i);
            List<String> list = new ArrayList<String>();

            for (int j = 0; j < 50000; j++) {
                list.add(j + "");
            }

            long t1 = System.nanoTime();
            list.get(0);
            long t2 = System.nanoTime();
            System.out.println("time (0) :: " + (t2 - t1));

            t1 = System.nanoTime();
            list.get(20000);
            t2 = System.nanoTime();
            System.out.println("time (20000) :: " + (t2 - t1));

            t1 = System.nanoTime();
            list.get(49000);
            t2 = System.nanoTime();
            System.out.println("time (49000) :: " + (t2 - t1));
        }

    }
}

Output:

============== Iteration ======= 1
time (0) :: 12000
time (20000) :: 1000
time (49000) :: 0
============== Iteration ======= 2
time (0) :: 17000
time (20000) :: 14000
time (49000) :: 16000
============== Iteration ======= 3
time (0) :: 2000
time (20000) :: 0
time (49000) :: 0
============== Iteration ======= 4
time (0) :: 3000
time (20000) :: 1000
time (49000) :: 0
============== Iteration ======= 5
time (0) :: 16000
time (20000) :: 8000
time (49000) :: 8000
============== Iteration ======= 6
time (0) :: 2000
time (20000) :: 0
time (49000) :: 0
============== Iteration ======= 7
time (0) :: 1000
time (20000) :: 1000
time (49000) :: 0
============== Iteration ======= 8
time (0) :: 4000
time (20000) :: 0
time (49000) :: 1000
============== Iteration ======= 9
time (0) :: 2000
time (20000) :: 0
time (49000) :: 0
============== Iteration ======= 10
time (0) :: 1000
time (20000) :: 1000
time (49000) :: 1000
============== Iteration ======= 11
time (0) :: 2000
time (20000) :: 0
time (49000) :: 0
============== Iteration ======= 12
time (0) :: 2000
time (20000) :: 0
time (49000) :: 0
============== Iteration ======= 13
time (0) :: 2000
time (20000) :: 1000
time (49000) :: 1000
============== Iteration ======= 14
time (0) :: 1000
time (20000) :: 0
time (49000) :: 0
============== Iteration ======= 15
time (0) :: 2000
time (20000) :: 0
time (49000) :: 0
============== Iteration ======= 16
time (0) :: 2000
time (20000) :: 0
time (49000) :: 0
============== Iteration ======= 17
time (0) :: 2000
time (20000) :: 0
time (49000) :: 0
============== Iteration ======= 18
time (0) :: 1000
time (20000) :: 1000
time (49000) :: 1000
============== Iteration ======= 19
time (0) :: 1000
time (20000) :: 1000
time (49000) :: 0
============== Iteration ======= 20
time (0) :: 2000
time (20000) :: 0
time (49000) :: 0
TheCodingFrog
  • 3,406
  • 3
  • 21
  • 27
  • 1
    It's unlikely that `System.nanotime` has enough resolution to measure a get, which probably only takes a handful of CPU cycles (if that)... Measure the time it takes to `list.get(0)` 1 million times instead. – assylias Jul 18 '15 at 16:02
  • Understood and Good point! - Thanks! – TheCodingFrog Jul 18 '15 at 16:19

0 Answers0