Advertisement
Guest User

Benchmark for double asterisk finder

a guest
Jul 28th, 2010
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.87 KB | None | 0 0
  1. // see http://stackoverflow.com/questions/3350804/how-to-check-if-a-string-contains-two-asterisk-characters
  2.  
  3. public class AsteriskBenchmark{
  4.  
  5.     public interface AsteriskFinder{
  6.  
  7.         String identifier();
  8.  
  9.         boolean testsForExactlyTwo();
  10.  
  11.         boolean hasTwoAsterisks(String str);
  12.     }
  13.  
  14.     public static class SeanizerAsteriskFinder implements AsteriskFinder{
  15.  
  16.         @Override
  17.         public boolean hasTwoAsterisks(final String str){
  18.             final String[] arr = ("_" + str + "_").split("\\*");
  19.             return arr.length == 3;
  20.         }
  21.  
  22.         @Override
  23.         public String identifier(){
  24.             return "seanizer";
  25.         }
  26.  
  27.         @Override
  28.         public boolean testsForExactlyTwo(){
  29.             return true;
  30.         }
  31.     }
  32.  
  33.     public static class MarkByersAsteriskFinder implements AsteriskFinder{
  34.  
  35.         @Override
  36.         public boolean hasTwoAsterisks(final String str){
  37.             return str.matches("^[^*]*(?:\\*[^*]*){2}$");
  38.         }
  39.  
  40.         @Override
  41.         public boolean testsForExactlyTwo(){
  42.             return true;
  43.         }
  44.  
  45.         @Override
  46.         public String identifier(){
  47.             return "Mark Byers";
  48.         }
  49.     }
  50.  
  51.     public static class JoachimSauerAsteriskFinder implements AsteriskFinder{
  52.  
  53.         @Override
  54.         public boolean testsForExactlyTwo(){
  55.             return false;
  56.         }
  57.  
  58.         @Override
  59.         public boolean hasTwoAsterisks(final String str){
  60.             final int asterisk1 = str.indexOf('*');
  61.             return asterisk1 != -1 && str.indexOf('*', asterisk1 + 1) != -1;
  62.  
  63.         }
  64.  
  65.         @Override
  66.         public String identifier(){
  67.             return "Joachim Sauer";
  68.         }
  69.     }
  70.  
  71.     public static class BhupsAsteriskFinder implements AsteriskFinder{
  72.  
  73.         @Override
  74.         public boolean testsForExactlyTwo(){
  75.             return false;
  76.         }
  77.  
  78.         @Override
  79.         public boolean hasTwoAsterisks(final String str){
  80.             int i;
  81.             if((i = str.indexOf("*")) != -1){
  82.                 if((i = str.indexOf("*", i + 1)) != -1){
  83.                     return true;
  84.                 }
  85.             }
  86.             return false;
  87.  
  88.         }
  89.  
  90.         @Override
  91.         public String identifier(){
  92.             return "bhups";
  93.         }
  94.     }
  95.  
  96.     public static class BozhoRegexAsteriskFinder implements AsteriskFinder{
  97.  
  98.         @Override
  99.         public boolean hasTwoAsterisks(final String str){
  100.             final String regex = ".*\\*.*\\*.*";
  101.             return str.matches(regex);
  102.  
  103.         }
  104.  
  105.         @Override
  106.         public boolean testsForExactlyTwo(){
  107.             return false;
  108.         }
  109.  
  110.         @Override
  111.         public String identifier(){
  112.             return "Bozho (regex version)";
  113.         }
  114.     }
  115.  
  116.     public static class BozhoLoopAsteriskFinder implements AsteriskFinder{
  117.  
  118.         @Override
  119.         public boolean testsForExactlyTwo(){
  120.             return true;
  121.         }
  122.  
  123.         @Override
  124.         public boolean hasTwoAsterisks(final String str){
  125.             int asterisks = 0;
  126.             for(int i = 0; i < str.length(); i++){
  127.                 if(str.charAt(i) == '*'){
  128.                     asterisks++;
  129.                 }
  130.             }
  131.             return asterisks == 2;
  132.         }
  133.  
  134.         @Override
  135.         public String identifier(){
  136.             return "Bozho (loop version)";
  137.         }
  138.     }
  139.  
  140.     public static class PolygenelubricantsAsteriskFinder implements
  141.         AsteriskFinder{
  142.  
  143.         @Override
  144.         public boolean testsForExactlyTwo(){
  145.             return false;
  146.         }
  147.  
  148.         @Override
  149.         public boolean hasTwoAsterisks(final String haystack){
  150.             final String needle = "*";
  151.             final int index = haystack.indexOf(needle);
  152.             // changed logic to do what the others do
  153.             return index > -1
  154.                 && haystack.indexOf(needle, index + 1) == haystack.lastIndexOf(needle);
  155.  
  156.         }
  157.  
  158.         @Override
  159.         public String identifier(){
  160.             return "polygenelubricants";
  161.         }
  162.     }
  163.  
  164.     public static void main(final String[] args){
  165.         final List<AsteriskFinder> finders = Arrays.asList(//
  166.         new BhupsAsteriskFinder(), //
  167.             new BozhoLoopAsteriskFinder(), //
  168.             new BozhoRegexAsteriskFinder(), //
  169.             new JoachimSauerAsteriskFinder(), //
  170.             new MarkByersAsteriskFinder(), //
  171.             new SeanizerAsteriskFinder()//
  172.         );
  173.  
  174.         final Collection<String> oneOrLessAsterisks =
  175.             createStringsWithOneOrLessAsterisks();
  176.         final Collection<String> exactlyTwoAsterisks =
  177.             createStringsWithExactlyTwoAsterisks();
  178.         final Collection<String> moreThanTwoAsterisks =
  179.             createStringsWithMoreThanTwoAsterisks();
  180.  
  181.         System.out.println("\n*********************************************************************************\n");
  182.         System.out.println("Testing strings with one or less asterisk");
  183.         for(final AsteriskFinder asteriskFinder : finders){
  184.             System.out.println("");
  185.             int errors = 0;
  186.             System.out.println("Processor: " + asteriskFinder.identifier());
  187.             final long start = System.currentTimeMillis();
  188.             for(int i = 0; i < 10000; i++){
  189.                 for(final String input : oneOrLessAsterisks){
  190.                     if(asteriskFinder.hasTwoAsterisks(input)){
  191.                         errors++;
  192.                     }
  193.                 }
  194.             }
  195.             System.out.println("Finished. Duration: "
  196.                 + (System.currentTimeMillis() - start) + " ms, errors: "
  197.                 + errors);
  198.         }
  199.  
  200.         System.out.println("\n*********************************************************************************\n");
  201.         System.out.println("Testing strings with exactly two asterisks");
  202.         for(final AsteriskFinder asteriskFinder : finders){
  203.             System.out.println("");
  204.             int errors = 0;
  205.             System.out.println("Processor: " + asteriskFinder.identifier());
  206.             final long start = System.currentTimeMillis();
  207.             for(int i = 0; i < 10000; i++){
  208.                 for(final String input : exactlyTwoAsterisks){
  209.                     if(!asteriskFinder.hasTwoAsterisks(input)){
  210.                         errors++;
  211.                     }
  212.                 }
  213.             }
  214.             System.out.println("Finished. Duration: "
  215.                 + (System.currentTimeMillis() - start) + " ms, errors: "
  216.                 + errors);
  217.         }
  218.  
  219.         System.out.println("\n*********************************************************************************\n");
  220.         System.out.println("Testing strings with more than two asterisks (not all processors will be included)");
  221.         for(final AsteriskFinder asteriskFinder : finders){
  222.             System.out.println("");
  223.             if(!asteriskFinder.testsForExactlyTwo()){
  224.                 System.out.println("Skipping processor "
  225.                     + asteriskFinder.identifier());
  226.                 continue;
  227.             }
  228.             int errors = 0;
  229.             System.out.println("Processor: " + asteriskFinder.identifier());
  230.             final long start = System.currentTimeMillis();
  231.             for(int i = 0; i < 10000; i++){
  232.                 for(final String input : moreThanTwoAsterisks){
  233.                     if(asteriskFinder.hasTwoAsterisks(input)){
  234.                         errors++;
  235.                     }
  236.                 }
  237.             }
  238.             System.out.println("Finished. Duration: "
  239.                 + (System.currentTimeMillis() - start) + " ms, errors: "
  240.                 + errors);
  241.         }
  242.     }
  243.  
  244.     private static Collection<String> createStringsWithExactlyTwoAsterisks(){
  245.         final String randomString1 =
  246.             insertAsterisk(insertAsterisk(createRandomWord()));
  247.         final String randomString2 =
  248.             insertAsterisk(insertAsterisk(createRandomWord()));
  249.         final String randomString3 =
  250.             insertAsterisk(insertAsterisk(createRandomWord()));
  251.  
  252.         return Arrays.asList(//
  253.         "ab*d*",
  254.             "*d*f",
  255.             "*hi*",
  256.             "jk*lmnop*qrst",
  257.             "uvwx**",
  258.             randomString1,
  259.             randomString2,
  260.             randomString3);
  261.     }
  262.  
  263.     private static Collection<String> createStringsWithMoreThanTwoAsterisks(){
  264.         String randomString1 = createRandomWord();
  265.         for(int i = 1; i < rand.nextInt(4) + 3; i++){
  266.             randomString1 = insertAsterisk(randomString1);
  267.         }
  268.         String randomString2 = createRandomWord();
  269.         for(int i = 1; i < rand.nextInt(4) + 3; i++){
  270.             randomString2 = insertAsterisk(randomString2);
  271.         }
  272.         String randomString3 = createRandomWord();
  273.         for(int i = 1; i < rand.nextInt(4) + 3; i++){
  274.             randomString3 = insertAsterisk(randomString3);
  275.         }
  276.  
  277.         return Arrays.asList(//
  278.         "ab*d*f*h",
  279.             "*d*f*",
  280.             "*hi**",
  281.             "jk*lmn*p*qrst",
  282.             "uvwx***",
  283.             randomString1,
  284.             randomString2,
  285.             randomString3);
  286.     }
  287.  
  288.     private static String insertAsterisk(final String word){
  289.         final int mark = rand.nextInt(word.length());
  290.         return word.substring(0, mark) + '*' + word.substring(mark);
  291.     }
  292.  
  293.     private static Random rand = new Random();
  294.  
  295.     private static String createRandomWord(){
  296.         final int size = rand.nextInt(500);
  297.         final char[] arr = new char[size];
  298.         for(int i = 0; i < size; i++){
  299.             arr[i] = (char) (65 + rand.nextInt(26));
  300.         }
  301.         return new String(arr);
  302.     }
  303.  
  304.     private static Collection<String> createStringsWithOneOrLessAsterisks(){
  305.         return Arrays.asList(//
  306.         "abc",
  307.             "d*f",
  308.             "*hi",
  309.             "jklmnopqrst",
  310.             "uvwxyz",
  311.             createRandomWord());
  312.     }
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement