fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. // http://e...content-available-to-author-only...c.de/congress/2011/Fahrplan/events/4680.en.html
  5.  
  6. public static void main(String[] args) {
  7. final String[] tbl1 = {"ab", "cd", "ef"};
  8. final String[] tbl2 = {"tt", "uU", "v6"};
  9. final int N = 40000;
  10.  
  11. String[] k1 = new String[N], k2 = new String[N];
  12. String[] v = new String[N];
  13. for (int i = 0; i < N; i++) {
  14. v[i] = Integer.toString(i, 3);
  15. k1[i] = v[i].replaceAll("0", tbl1[0]).replaceAll("1", tbl1[1]).replaceAll("2", tbl1[2]);
  16. k2[i] = v[i].replaceAll("0", tbl2[0]).replaceAll("1", tbl2[1]).replaceAll("2", tbl2[2]);
  17. }
  18.  
  19. System.out.printf("TreeMap tbl1: %d ms\n", time(new TreeMap<String, String>(), k1, v));
  20. System.out.printf("TreeMap tbl2: %d ms\n", time(new TreeMap<String, String>(), k2, v));
  21. System.out.printf("HashMap tbl1: %d ms\n", time(new HashMap<String, String>(), k1, v));
  22. System.out.printf("HashMap tbl2: %d ms\n", time(new HashMap<String, String>(), k2, v));
  23. }
  24.  
  25. static long time(Map<String,String> m, String[] k, String[] v) {
  26. int N = k.length;
  27. long begin = System.currentTimeMillis();
  28. for (int i = 0; i < N; i++) {
  29. m.put(k[i], v[i]);
  30. }
  31. long end = System.currentTimeMillis();
  32. return end - begin;
  33. }
  34. }
  35.  
Success #stdin #stdout 9.24s 212992KB
stdin
Standard input is empty
stdout
TreeMap tbl1: 44 ms
TreeMap tbl2: 45 ms
HashMap tbl1: 61 ms
HashMap tbl2: 8179 ms