0

I want to create method in to see how much time it takes to create 10000 of ArrayList and LinkedList. What am i looking for?

public class Solution
{
    public static void main(String[] args)
    {
        System.out.println(getTimeMsOfInsert(new ArrayList()));

        System.out.println(getTimeMsOfInsert(new LinkedList()));
    }

    public static long  getTimeMsOfInsert(List list)
    {
        insert10000(list);
    }

    public static void insert10000(List list)
    {
        for (int i=0;i<10000;i++)
        {
            list.add(0, new Object());
        }
    }
}
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Predict_it
  • 247
  • 2
  • 4
  • 13

2 Answers2

4

Your code have a compilation errors, so i fixed them and added some code may be help you.

so you could do this:

public class Solution{

public static void main(String[] args) {
    System.out.println(getTimeMsOfInsert(new ArrayList()));
    System.out.println(getTimeMsOfInsert(new LinkedList()));
}

public static long getTimeMsOfInsert(List list) {
    return getInsertTime(list);
}

public static long getInsertTime(List list) {
    long start = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
        list.add(new Object());
    }
    return System.currentTimeMillis() - start;
}
}
Salah
  • 8,567
  • 3
  • 26
  • 43
  • Don’t use `System.currentTimeMillis()` for measuring elapsed time. This clock may jump— even backwards— when the system’s clock is being adjusted, e.g. via NTP. Use [`System.nanoTime()`](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime()) for measuring elapsed time. – Holger Feb 18 '14 at 16:06
0

Hello from codegym community:) Below is my solution. You can easily use System.currentTimeMillis()

   public static long getInsertTimeInMs(List list) {
    // write your code here
     long start = System.currentTimeMillis();
    insert10000(list);

    // write your code here
    long finish = System.currentTimeMillis();
    return  finish - start;
}
chety
  • 136
  • 4