How to display execution times

Question from Abdul#4709

Stuck on LinkedLists

Anyone knows how to display execution times

Probably out of scope for your assignment, but you can always use something like this

private void timeProcedure(Runnable procedure) {
    long start = System.currentTimeMillis();
    procedure.run();
    long end = System.currentTimeMillis();
    long duration = end - start;
    System.out.println(duration + " milliseconds.");
}

and then you can do something like

timeProcedure(() -> linkedList.get(1000000 / 2);

You can also add the name to have better debugging

private void timeProcedure(String procedureName, Runnable procedure) {
    long start = System.currentTimeMillis();
    procedure.run();
    long end = System.currentTimeMillis();
    long duration = end - start;
    System.out.println(procedureName + " took " + duration + " milliseconds.");
}
timeProcedure("Accessing an element in the middle of a linked list", () -> linkedList.get(100000 / 2))

which would output something like

Accessing an element in the middle of a linked list took 33 milliseconds

<- Index