fork download
  1. import java.util.*;
  2.  
  3. class Main
  4. {
  5. private static final int N = 10000000;
  6.  
  7. public static void main(String[] args)
  8. {
  9. testArrayList();
  10. testLinkedList();
  11. }
  12.  
  13. public static void testArrayList()
  14. {
  15. ArrayList<Object> liste = new ArrayList<Object>();
  16. Object o = new Object();
  17. System.gc();
  18. long before = System.nanoTime();
  19. for (int i = N; i > 0; --i)
  20. {
  21. liste.add(o);
  22. }
  23. long after = System.nanoTime();
  24. long delta = after - before;
  25. double seconds = delta / 1000000000.0;
  26. System.out.printf("ArrayList: %.2f seconds%n", seconds);
  27. }
  28.  
  29. public static void testLinkedList()
  30. {
  31. LinkedList<Object> liste = new LinkedList<Object>();
  32. Object o = new Object();
  33. System.gc();
  34. long before = System.nanoTime();
  35. for (int i = N; i > 0; --i)
  36. {
  37. liste.add(o);
  38. }
  39. long after = System.nanoTime();
  40. long delta = after - before;
  41. double seconds = delta / 1000000000.0;
  42. System.out.printf("LinkedList: %.2f seconds%n", seconds);
  43. }
  44. }
  45.  
Success #stdin #stdout 2.24s 380160KB
stdin
Standard input is empty
stdout
ArrayList:  0.59 seconds
LinkedList: 1.56 seconds