Created
August 14, 2014 09:38
-
-
Save vlad-at-yteq/f3680dc2b07bb70f205a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.regex.Pattern; | |
import org.apache.commons.lang.RandomStringUtils; | |
import org.apache.commons.lang.StringUtils; | |
import org.apache.commons.lang.math.RandomUtils; | |
import static java.lang.String.*; | |
import static java.util.Arrays.*; | |
public class Temp { | |
public static void main(String[] args) { | |
final List<String> needles = asList( | |
"file", "apple", "orange", "banana", "folder", "tree", "stock", "hashmap", "useful", "interesting", "exciting", | |
"file", "apple", "orange", "banana", "folder", "tree", "stock", "hashmap", "useful", "interesting", "exciting", | |
"file", "apple", "orange", "banana", "folder", "tree", "stock", "hashmap", "useful", "interesting", "exciting"); | |
final List<String> hay = generateHay(needles); | |
int manyIterations = 100000; | |
measure("containsAny", manyIterations, new Action() { | |
public void doIt() { | |
for (String s : hay) { | |
containsAny(s, needles); | |
} | |
} | |
}); | |
measure("containsAnyWithRegex", manyIterations, new Action() { | |
public void doIt() { | |
for (String s : hay) { | |
containsAnyWithRegex(s, needles); | |
} | |
} | |
}); | |
measure("containsAnyWithOffset", manyIterations, new Action() { | |
public void doIt() { | |
for (String s : hay) { | |
containsAnyWithOffset(s, needles); | |
} | |
} | |
}); | |
} | |
private static List<String> generateHay(List<String> needles) { | |
final List<String> hay = new ArrayList<>(); | |
for (int i = 0; i < 20; i++) { | |
String s = RandomStringUtils.randomAlphanumeric(10); | |
for (int k = 0; k < RandomUtils.nextInt(3); k++) { | |
String word = needles.get(RandomUtils.nextInt(needles.size())); | |
int splitPos = RandomUtils.nextInt(s.length()); | |
s = s.substring(0, splitPos) + word + s.substring(splitPos); | |
} | |
hay.add(s); | |
} | |
return hay; | |
} | |
public static boolean containsAnyWithOffset(String searchString, List<String> searchCollection) { | |
for (int offset = 0; offset < searchString.length(); offset++) { | |
for (String sought: searchCollection) { | |
int remainder = searchString.length() - offset; | |
if (remainder >= sought.length() && searchString.startsWith(sought, offset)) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
static Pattern PATTERN; | |
public static boolean containsAnyWithRegex(String searchString, List<String> searchCollection) { | |
if (PATTERN == null) { | |
final StringBuilder pattern = new StringBuilder(); | |
for (String sought : searchCollection) { | |
if (!StringUtils.isEmpty(sought)) { | |
if (pattern.length() != 0) { | |
pattern.append('|'); | |
} | |
pattern.append(Pattern.quote(sought)); | |
} | |
} | |
PATTERN = Pattern.compile("(" + pattern + ")"); | |
} | |
return PATTERN.matcher(searchString).find(); | |
} | |
public static boolean containsAny(String searchString, List<String> searchCollection) { | |
int size = searchCollection.size(); | |
for (int i = 0; i < size; i++) { | |
String stringInCollection = searchCollection.get(i); | |
if (!StringUtils.isEmpty(stringInCollection)) { | |
if (searchString.indexOf(stringInCollection, 0) > -1) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
static void measure(String name, int n, Action action) { | |
long start = System.currentTimeMillis(); | |
for(int i = 0; i < n; i++) { | |
action.doIt(); | |
} | |
long time = System.currentTimeMillis() - start; | |
System.out.println(format("Completed %s %s in %s ms (avg %s ms)", n, name, time, time / n)); | |
} | |
static abstract class Action implements Runnable { | |
@Override | |
public void run() { | |
try { | |
doIt(); | |
} catch (Exception e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
public abstract void doIt(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment