Changeset 76 for collected/trunk

Show
Ignore:
Timestamp:
06/03/08 19:40:32 (7 months ago)
Author:
simon
Message:

Made the example runner more configurable for performing timings.

Location:
collected/trunk/java-dsu
Files:
2 added
2 modified

Legend:

Unmodified
Added
Removed
  • collected/trunk/java-dsu

    • Property svn:ignore set to
      bin
      .classpath
      .project
  • collected/trunk/java-dsu/src/com/bitstructures/DSUExample2.java

    r71 r76  
    3333        } 
    3434 
    35         private static void dsu(String filename) throws IOException { 
     35        private static void dsu(List<String> words, boolean count, boolean log) 
     36                        throws IOException { 
    3637                final Counter c = new Counter(); 
    37                 List<String> a = readWords(filename); 
    3838                long t1 = System.currentTimeMillis(); 
    39                 DSU.sort(a, new SortKey<String,String>() { 
    40                         public String getValue(String s) { 
    41                                 c.increment(); 
    42                                 return s.toLowerCase(); 
     39                if (count) { 
     40                        DSU.sort(words, new SortKey<String,String>() { 
     41                                public String getValue(String s) { 
     42                                        c.increment(); 
     43                                        return s.toLowerCase(); 
     44                                } 
     45                        }); 
     46                } else { 
     47                        DSU.sort(words, new SortKey<String,String>() { 
     48                                public String getValue(String s) { 
     49                                        return s.toLowerCase(); 
     50                                } 
     51                        }); 
     52                } 
     53                long t2 = System.currentTimeMillis(); 
     54                if (log) { 
     55                        System.out.println("DSU"); 
     56                        if (count) { 
     57                                System.out.println("called " + c.i + " times"); 
     58                        } else { 
     59                                System.out.println((t2 - t1) + "ms"); 
    4360                        } 
    44                 }); 
    45                 long t2 = System.currentTimeMillis(); 
    46 //              for (String s : a) { 
    47 //                      System.out.println(s); 
    48 //              } 
    49                 System.out.println("DSU"); 
    50                 System.out.println("called " + c.i + " times"); 
    51                 System.out.println((t2 - t1) + " millis"); 
     61                } 
    5262        } 
    5363 
    54         private static void withComparator(String filename) throws IOException { 
     64        private static void collectionsSortWithComparator(List<String> words, 
     65                        boolean count, boolean log) throws IOException { 
    5566                final Counter c = new Counter(); 
    56                 List<String> a = readWords(filename); 
    5767                long t1 = System.currentTimeMillis(); 
    58                 Collections.sort(a, new Comparator<String>() { 
    59                         public int compare(String s1, String s2) { 
    60                                 c.increment(); 
    61                                 return s1.toLowerCase().compareTo(s2.toLowerCase()); 
     68                if (count) { 
     69                        Collections.sort(words, new Comparator<String>() { 
     70                                public int compare(String s1, String s2) { 
     71                                        c.increment(); 
     72                                        return s1.toLowerCase().compareTo(s2.toLowerCase()); 
     73                                } 
     74                        }); 
     75                } else { 
     76                        Collections.sort(words, new Comparator<String>() { 
     77                                public int compare(String s1, String s2) { 
     78                                        return s1.toLowerCase().compareTo(s2.toLowerCase()); 
     79                                } 
     80                        }); 
     81                } 
     82                long t2 = System.currentTimeMillis(); 
     83                if (log) { 
     84                        System.out.println("Collections.sort with Comparator"); 
     85                        if (count) { 
     86                                System.out.println("called " + c.i + " times"); 
     87                        } else { 
     88                                System.out.println((t2 - t1) + "ms"); 
    6289                        } 
    63                 }); 
    64                 long t2 = System.currentTimeMillis(); 
    65 //              for (String s : a) { 
    66 //                      System.out.println(s); 
    67 //              } 
    68                 System.out.println("Collections.sort"); 
    69                 System.out.println("called " + c.i + " times"); 
    70                 System.out.println((t2 - t1) + " millis"); 
     90                } 
    7191        } 
    7292 
    7393        public static void main(String[] args) throws IOException { 
    74                 dsu(args[0]); 
    75                 withComparator(args[0]); 
     94                int DSU = 0; 
     95                int COMPARATOR = 1; 
     96                int which = DSU; 
     97                boolean count = false; 
     98                boolean outputWords = false; 
     99                if (args.length < 1) { 
     100                        System.err.println("usage: DSUExample2 OPTIONS FILENAME"); 
     101                        System.exit(1); 
     102                } 
     103                for (int i = 0; i < args.length - 1; i++) { 
     104                        if (args[i].equals("-dsu")) { 
     105                                which = DSU; 
     106                        } 
     107                        if (args[i].equals("-comparator")) { 
     108                                which = COMPARATOR; 
     109                        } 
     110                        if (args[i].equals("-count")) { 
     111                                count = true; 
     112                        } 
     113                        if (args[i].equals("-words")) { 
     114                                outputWords = true; 
     115                        } 
     116                } 
     117                List<String> words = readWords(args[args.length - 1]); 
     118                if (which == DSU) { 
     119                        dsu(words, count, !outputWords); 
     120                } else if (which == COMPARATOR) { 
     121                        collectionsSortWithComparator(words, count, !outputWords); 
     122                } 
     123                if (outputWords) { 
     124                        for (String s : words) { 
     125                                System.out.println(s); 
     126                        } 
     127                } 
    76128        } 
    77129}