fork download
  1. import java.util.*;
  2. import java.text.*;
  3. class ListTest {
  4. private void test(String label, Runnable r) {
  5. long start = System.nanoTime();
  6. r.run();
  7. long end = System.nanoTime();
  8. System.out.println(label + NumberFormat.getNumberInstance().format(end - start) + " ns");
  9. }
  10. private ListTest(List<Object> list, int n) {
  11. Random r = new Random();
  12. System.out.println("class = " + list.getClass().getName() + ", n = " + n);
  13. list.add(null);
  14. test("(nop): ", () -> {for (int i = 1; i < n; i++) /*nop*/;});
  15. test("add(int, Obj): ", () -> {for (int i = 1; i < n; i++) list.add(r.nextInt(list.size()), null);});
  16. test("remove(int): ", () -> {for (int i = 0; i < n; i++) list.remove(r.nextInt(list.size()));});
  17. System.out.println("");
  18. }
  19. public static void main(String[] args) {
  20. int n = 10000;
  21. new ListTest(new ArrayList<>(), n);
  22. new ListTest(new LinkedList<>(), n);
  23. new ListTest(new Vector<>(), n);
  24. }
  25. }
  26.  
Success #stdin #stdout 0.42s 321472KB
stdin
Standard input is empty
stdout
class = java.util.ArrayList, n = 10000
(nop): 135,906 ns
add(int, Obj): 18,021,541 ns
remove(int): 13,711,185 ns

class = java.util.LinkedList, n = 10000
(nop): 332,831 ns
add(int, Obj): 60,957,543 ns
remove(int): 73,760,127 ns

class = java.util.Vector, n = 10000
(nop): 13,875 ns
add(int, Obj): 14,875,156 ns
remove(int): 12,211,621 ns