package labs.arraylist;

public class TimeIt {

  public static void main(String[] args) {
    runTimeTests();
  }

  public static void functionToTest(MyArrayList<Integer> list) {
    // REPLACE the following code with the code you want to test!
    
    list.add(1701);
  }

  ////////// DO NOT MODIFY ANYTHING BELOW THIS LINE //////////

  public static void runTimeTests() {
    timeIt(100, false); // Warm up the JVM

    timeIt(1); // Run with only 1 element already in the list
    timeIt(2); // Run with 2 elements in the list
    timeIt(5); // Run with 5 elements in the list
    timeIt(10); // etc.
    timeIt(20);
    timeIt(30);
    timeIt(40);
    timeIt(50);
    timeIt(60);
    timeIt(70);
    timeIt(80);
    timeIt(90);
    timeIt(100);
  }

  public static void timeIt(int listSize) {
    timeIt(listSize, true);
  }

  public static void timeIt(int listSize, boolean print) {
    MyArrayList<Integer> list = createList(listSize);
    long duration = timeSingle(list);
    if (print) {
      System.out.println("(" + listSize + ", " + duration / 1000.0 + ")"); // Time in microseconds
    }
  }

  public static MyArrayList<Integer> createList(int n) {
    MyArrayList<Integer> list = new MyArrayList<>();
    for (int i = 0; i < n; i++) {
      // Fill the list with random numbers
      list.add((int) (Math.random() * n));
    }
    return list;
  }

  public static long timeSingle(MyArrayList<Integer> list) {
    long startTime = System.nanoTime();

    functionToTest(list);

    long endTime = System.nanoTime();
    long duration = endTime - startTime;
    return duration;
  }
}
