Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashSet;
- import java.util.Random;
- import java.util.Set;
- import java.util.regex.Pattern;
- public class StringSearchTest {
- private static final int NUMBER_OF_RUNS = 10000;
- private static final int NUMBER_OF_ENTRIES = 6000;
- private static final int WORD_LENGTH = 6;
- private static char[] symbols;
- private static Random random = new Random();
- static {
- StringBuilder tmp = new StringBuilder();
- for (char ch = '0'; ch <= '9'; ++ch) {
- tmp.append(ch);
- }
- for (char ch = 'a'; ch <= 'z'; ++ch) {
- tmp.append(ch);
- }
- symbols = tmp.toString().toCharArray();
- }
- protected static String[] buildWords(int count) {
- String[] words = new String[count];
- for (int i = 0; i < count; i++) {
- words[i] = generateRandomWord(WORD_LENGTH);
- }
- return words;
- }
- protected static void testLoop(String[] words, String inputWord) {
- for (String word : words) {
- word.equals(inputWord);
- }
- }
- protected static void testRegex(Pattern pattern, String inputWord) {
- pattern.matcher(inputWord).matches();
- }
- protected static void testRegex(String[] words, String inputWord) {
- testRegex(compileRegex(words), inputWord);
- }
- protected static Pattern compileRegex(String[] words) {
- final StringBuilder regexBuffer = new StringBuilder("(");
- for (int i = 0; i < words.length; i++) {
- regexBuffer.append(words[i]);
- if (i != words.length - 1) {
- regexBuffer.append("|");
- }
- }
- return Pattern.compile(regexBuffer.append(")").toString());
- }
- public static void main(String[] args) {
- String[] words = buildWords(NUMBER_OF_ENTRIES);
- long[] regexRuntimes = new long[NUMBER_OF_RUNS];
- for (int i = 0; i < NUMBER_OF_RUNS; i++) {
- long startTime = System.currentTimeMillis();
- testRegex(words, generateRandomWord(WORD_LENGTH));
- regexRuntimes[i] = System.currentTimeMillis() - startTime;
- }
- Pattern pattern = compileRegex(words);
- long[] precompileRegex = new long[NUMBER_OF_RUNS];
- for (int i = 0; i < NUMBER_OF_RUNS; i++) {
- long startTime = System.currentTimeMillis();
- testRegex(pattern, generateRandomWord(WORD_LENGTH));
- precompileRegex[i] = System.currentTimeMillis() - startTime;
- }
- long[] loopRuntimes = new long[NUMBER_OF_RUNS];
- for (int i = 0; i < NUMBER_OF_RUNS; i++) {
- long startTime = System.currentTimeMillis();
- testLoop(words, generateRandomWord(WORD_LENGTH));
- loopRuntimes[i] = System.currentTimeMillis() - startTime;
- }
- Set<String> wordSet = new HashSet<>(NUMBER_OF_ENTRIES);
- for (String word : words) {
- wordSet.add(word);
- }
- long[] setContainsRuntimes = new long[NUMBER_OF_RUNS];
- for (int i = 0; i < NUMBER_OF_RUNS; i++) {
- long startTime = System.currentTimeMillis();
- wordSet.contains(generateRandomWord(WORD_LENGTH));
- setContainsRuntimes[i] = System.currentTimeMillis() - startTime;
- }
- System.out.println(String.format("On-Demand Regex: %6d (%.2f avg)", total(regexRuntimes), average(regexRuntimes)));
- System.out.println(String.format("Pre-compile Regex: %6d (%.2f avg)", total(precompileRegex), average(precompileRegex)));
- System.out.println(String.format("Loop: %6d (%.2f avg)", total(loopRuntimes), average(loopRuntimes)));
- System.out.println(String.format("Set.contains: %6d (%.2f avg)", total(setContainsRuntimes), average(setContainsRuntimes)));
- }
- protected static Long total(long[] input) {
- Long total = 0l;
- for (long item : input) {
- total += item;
- }
- return total;
- }
- protected static double average(long[] input) {
- return total(input).doubleValue() / input.length;
- }
- protected static String generateRandomWord(int length) {
- char[] buffer = new char[length];
- for (int i = 0; i < length; i++) {
- buffer[i] = symbols[random.nextInt(symbols.length)];
- }
- return new String(buffer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement