Guest User

Untitled

a guest
Dec 10th, 2009
2,166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.UUID;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.         boolean useArrayList = args.length == 0 || !Boolean.parseBoolean(args[0]);
  11.        
  12.         List<String> list = useArrayList ? new ArrayList<String>() : new LinkedList<String>();
  13.         for (int i = 0; i < 100000; i++) {
  14.             list.add(UUID.randomUUID().toString());
  15.         }
  16.  
  17.         int sum = 0;
  18.  
  19.         long start = System.currentTimeMillis();
  20.         Iterator<String> iter = list.iterator();
  21.         while (iter.hasNext()) {
  22.             String s = iter.next();
  23.             sum += s.length();
  24.         }
  25.         System.out.println(System.currentTimeMillis() - start);
  26.  
  27.         start = System.currentTimeMillis();
  28.         for (String s : list) {
  29.             sum += s.length();
  30.         }
  31.         System.out.println(System.currentTimeMillis() - start);
  32.  
  33.         start = System.currentTimeMillis();
  34.         for (int i = 0; i < list.size(); i++) {
  35.             String s = list.get(i);
  36.             sum += s.length();
  37.         }
  38.         System.out.println(System.currentTimeMillis() - start);
  39.     }
  40. }
  41.  
RAW Paste Data