Advertisement
Guest User

Untitled

a guest
Apr 27th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.Random;
  3. import java.util.Set;
  4. import java.util.regex.Pattern;
  5.  
  6. public class StringSearchTest {
  7.  
  8.     private static final int NUMBER_OF_RUNS = 10000;
  9.  
  10.     private static final int NUMBER_OF_ENTRIES = 6000;
  11.     private static final int WORD_LENGTH = 6;
  12.  
  13.     private static char[] symbols;
  14.     private static Random random = new Random();
  15.  
  16.     static {
  17.         StringBuilder tmp = new StringBuilder();
  18.  
  19.         for (char ch = '0'; ch <= '9'; ++ch) {
  20.             tmp.append(ch);
  21.         }
  22.  
  23.         for (char ch = 'a'; ch <= 'z'; ++ch) {
  24.             tmp.append(ch);
  25.         }
  26.  
  27.         symbols = tmp.toString().toCharArray();
  28.     }
  29.  
  30.     protected static String[] buildWords(int count) {
  31.         String[] words = new String[count];
  32.         for (int i = 0; i < count; i++) {
  33.             words[i] = generateRandomWord(WORD_LENGTH);
  34.         }
  35.         return words;
  36.     }
  37.  
  38.     protected static void testLoop(String[] words, String inputWord) {
  39.         for (String word : words) {
  40.             word.equals(inputWord);
  41.         }
  42.     }
  43.  
  44.     protected static void testRegex(Pattern pattern, String inputWord) {
  45.         pattern.matcher(inputWord).matches();
  46.     }
  47.  
  48.     protected static void testRegex(String[] words, String inputWord) {
  49.         testRegex(compileRegex(words), inputWord);
  50.     }
  51.  
  52.     protected static Pattern compileRegex(String[] words) {
  53.         final StringBuilder regexBuffer = new StringBuilder("(");
  54.         for (int i = 0; i < words.length; i++) {
  55.             regexBuffer.append(words[i]);
  56.  
  57.             if (i != words.length - 1) {
  58.                 regexBuffer.append("|");
  59.             }
  60.         }
  61.  
  62.         return Pattern.compile(regexBuffer.append(")").toString());
  63.     }
  64.  
  65.     public static void main(String[] args) {
  66.         String[] words = buildWords(NUMBER_OF_ENTRIES);
  67.  
  68.         long[] regexRuntimes = new long[NUMBER_OF_RUNS];
  69.         for (int i = 0; i < NUMBER_OF_RUNS; i++) {
  70.             long startTime = System.currentTimeMillis();
  71.             testRegex(words, generateRandomWord(WORD_LENGTH));
  72.             regexRuntimes[i] = System.currentTimeMillis() - startTime;
  73.         }
  74.  
  75.         Pattern pattern = compileRegex(words);
  76.         long[] precompileRegex = new long[NUMBER_OF_RUNS];
  77.         for (int i = 0; i < NUMBER_OF_RUNS; i++) {
  78.             long startTime = System.currentTimeMillis();
  79.             testRegex(pattern, generateRandomWord(WORD_LENGTH));
  80.             precompileRegex[i] = System.currentTimeMillis() - startTime;
  81.         }
  82.  
  83.         long[] loopRuntimes = new long[NUMBER_OF_RUNS];
  84.         for (int i = 0; i < NUMBER_OF_RUNS; i++) {
  85.             long startTime = System.currentTimeMillis();
  86.             testLoop(words, generateRandomWord(WORD_LENGTH));
  87.             loopRuntimes[i] = System.currentTimeMillis() - startTime;
  88.         }
  89.  
  90.         Set<String> wordSet = new HashSet<>(NUMBER_OF_ENTRIES);
  91.         for (String word : words) {
  92.             wordSet.add(word);
  93.         }
  94.  
  95.         long[] setContainsRuntimes = new long[NUMBER_OF_RUNS];
  96.         for (int i = 0; i < NUMBER_OF_RUNS; i++) {
  97.             long startTime = System.currentTimeMillis();
  98.             wordSet.contains(generateRandomWord(WORD_LENGTH));
  99.             setContainsRuntimes[i] = System.currentTimeMillis() - startTime;
  100.         }
  101.  
  102.         System.out.println(String.format("On-Demand Regex:   %6d (%.2f avg)", total(regexRuntimes), average(regexRuntimes)));
  103.         System.out.println(String.format("Pre-compile Regex: %6d (%.2f avg)", total(precompileRegex), average(precompileRegex)));
  104.         System.out.println(String.format("Loop:              %6d (%.2f avg)", total(loopRuntimes), average(loopRuntimes)));
  105.         System.out.println(String.format("Set.contains:      %6d (%.2f avg)", total(setContainsRuntimes), average(setContainsRuntimes)));
  106.     }
  107.  
  108.     protected static Long total(long[] input) {
  109.         Long total = 0l;
  110.         for (long item : input) {
  111.             total += item;
  112.         }
  113.         return total;
  114.     }
  115.  
  116.     protected static double average(long[] input) {
  117.         return total(input).doubleValue() / input.length;
  118.     }
  119.  
  120.     protected static String generateRandomWord(int length) {
  121.         char[] buffer = new char[length];
  122.         for (int i = 0; i < length; i++) {
  123.             buffer[i] = symbols[random.nextInt(symbols.length)];
  124.         }
  125.         return new String(buffer);
  126.     }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement