| 12 | | public class DSUExample2 { |
| 13 | | private static class Counter { |
| 14 | | public int i = 0; |
| 15 | | public void increment() { |
| 16 | | i++; |
| 17 | | } |
| 18 | | } |
| | 19 | private static List<String> readWords(String filename) throws IOException { |
| | 20 | BufferedReader r = new BufferedReader(new FileReader(filename)); |
| | 21 | List<String> words = new ArrayList<String>(); |
| | 22 | String line = r.readLine(); |
| | 23 | while(line != null) { |
| | 24 | String[] wordsThisLine = line.split("\\s"); |
| | 25 | for (String word : wordsThisLine) { |
| | 26 | words.add(word); |
| | 27 | } |
| | 28 | line = r.readLine(); |
| | 29 | } |
| | 30 | return words; |
| | 31 | } |
| 20 | | private static List<String> readWords(String filename) throws IOException { |
| 21 | | BufferedReader r = new BufferedReader(new FileReader(filename)); |
| 22 | | List<String> words = new ArrayList<String>(); |
| 23 | | String line = r.readLine(); |
| 24 | | while(line != null) { |
| 25 | | String[] wordsThisLine = line.split("\\s"); |
| 26 | | for (String word : wordsThisLine) { |
| 27 | | words.add(word); |
| 28 | | } |
| 29 | | line = r.readLine(); |
| 30 | | } |
| 31 | | return words; |
| 32 | | } |
| | 33 | private static void dsu(String filename) throws IOException { |
| | 34 | final Counter c = new Counter(); |
| | 35 | List<String> a = readWords(filename); |
| | 36 | long t1 = System.currentTimeMillis(); |
| | 37 | DSU.sort(a, new ComparableExtractor<String,String>() { |
| | 38 | public String extract(String s) { |
| | 39 | c.increment(); |
| | 40 | return s.toLowerCase(); |
| | 41 | } |
| | 42 | }); |
| | 43 | long t2 = System.currentTimeMillis(); |
| | 44 | // for (String s : a) { |
| | 45 | // System.out.println(s); |
| | 46 | // } |
| | 47 | System.out.println("DSU"); |
| | 48 | System.out.println("called " + c.i + " times"); |
| | 49 | System.out.println((t2 - t1) + " millis"); |
| | 50 | } |
| 34 | | private static void dsu(String filename) throws IOException { |
| 35 | | final Counter c = new Counter(); |
| 36 | | List<String> a = readWords(filename); |
| 37 | | long t1 = System.currentTimeMillis(); |
| 38 | | DSU.sort(a, new ComparableExtractor<String,String>() { |
| 39 | | public String extract(String s) { |
| 40 | | c.increment(); |
| 41 | | return s.toLowerCase(); |
| 42 | | } |
| 43 | | }); |
| 44 | | long t2 = System.currentTimeMillis(); |
| 45 | | // for (String s : a) { |
| 46 | | // System.out.println(s); |
| 47 | | // } |
| 48 | | System.out.println("DSU"); |
| 49 | | System.out.println("called " + c.i + " times"); |
| 50 | | System.out.println((t2 - t1) + " millis"); |
| 51 | | } |
| | 52 | private static void withComparator(String filename) throws IOException { |
| | 53 | final Counter c = new Counter(); |
| | 54 | List<String> a = readWords(filename); |
| | 55 | long t1 = System.currentTimeMillis(); |
| | 56 | Collections.sort(a, new Comparator<String>() { |
| | 57 | public int compare(String s1, String s2) { |
| | 58 | c.increment(); |
| | 59 | return s1.toLowerCase().compareTo(s2.toLowerCase()); |
| | 60 | } |
| | 61 | }); |
| | 62 | long t2 = System.currentTimeMillis(); |
| | 63 | // for (String s : a) { |
| | 64 | // System.out.println(s); |
| | 65 | // } |
| | 66 | System.out.println("Collections.sort"); |
| | 67 | System.out.println("called " + c.i + " times"); |
| | 68 | System.out.println((t2 - t1) + " millis"); |
| | 69 | } |
| 53 | | private static void withComparator(String filename) throws IOException { |
| 54 | | final Counter c = new Counter(); |
| 55 | | List<String> a = readWords(filename); |
| 56 | | long t1 = System.currentTimeMillis(); |
| 57 | | Collections.sort(a, new Comparator<String>() { |
| 58 | | public int compare(String s1, String s2) { |
| 59 | | c.increment(); |
| 60 | | return s1.toLowerCase().compareTo(s2.toLowerCase()); |
| 61 | | } |
| 62 | | }); |
| 63 | | long t2 = System.currentTimeMillis(); |
| 64 | | // for (String s : a) { |
| 65 | | // System.out.println(s); |
| 66 | | // } |
| 67 | | System.out.println("Collections.sort"); |
| 68 | | System.out.println("called " + c.i + " times"); |
| 69 | | System.out.println((t2 - t1) + " millis"); |
| 70 | | } |
| 71 | | |
| 72 | | public static void main(String[] args) throws IOException { |
| 73 | | dsu("10940.txt"); |
| 74 | | withComparator("10940.txt"); |
| 75 | | } |
| | 71 | public static void main(String[] args) throws IOException { |
| | 72 | dsu("10940.txt"); |
| | 73 | withComparator("10940.txt"); |
| | 74 | } |