Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 31st, 2011  |  syntax: Java  |  size: 1.25 KB  |  views: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class ListBenchmark {
  2.  
  3.     private List<String> removeEvenFromLinkedList() {
  4.         List<String> list = new LinkedList<String>();
  5.         populate(list);
  6.         removeEven(list, "LinkedList");
  7.         return list;
  8.     }
  9.  
  10.     private List<String> removeEvenFromArrayList() {
  11.         List<String> list = new ArrayList<String>();
  12.         populate(list);
  13.         removeEven(list, "ArrayList");
  14.         return list;
  15.     }
  16.  
  17.     private void removeEven(List<String> list, String description) {
  18.         long t0 = System.nanoTime();
  19.         int i = 0;
  20.         for (Iterator<String> it = list.iterator(); it.hasNext(); i++) {
  21.             if (i % 2 == 0) {
  22.                 it.next();
  23.                 it.remove();
  24.             }
  25.         }
  26.         long t1 = System.nanoTime();
  27.         System.out.println(description + " : " + (t1 - t0));
  28.     }
  29.  
  30.     private void populate(List<String> list) {
  31.         for (int i = 0; i < 10_000; i++) {
  32.            list.add("hello");
  33.         }
  34.     }
  35.  
  36.     public static void main(String[] args) {
  37.         ListBenchmark benchmark = new ListBenchmark();
  38.         for (int i = 0; i < 100; i++) {
  39.             benchmark.removeEvenFromArrayList();
  40.             benchmark.removeEvenFromLinkedList();
  41.         }
  42.     }
  43. }
  44.  
clone this paste RAW Paste Data