Skip to content

Instantly share code, notes, and snippets.

@ignatov
Created March 31, 2019 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ignatov/07261e4439e3d4320a8129a4151bef6d to your computer and use it in GitHub Desktop.
Save ignatov/07261e4439e3d4320a8129a4151bef6d to your computer and use it in GitHub Desktop.
A test for todo and fixme patterns
import java.security.SecureRandom;
import java.util.regex.Pattern;
public class MatcherTest {
private static final String ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
private static final SecureRandom RANDOM = new SecureRandom();
private static String generate(int count) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; ++i) {
sb.append(ALPHABET.charAt(RANDOM.nextInt(ALPHABET.length())));
}
return sb.toString();
}
public static void main(String[] args) {
String text = generate(20_000_000);
long load = System.currentTimeMillis();
single(text);
System.out.println("composite " + (System.currentTimeMillis() - load));
long load2 = System.currentTimeMillis();
two(text);
System.out.println("separate " + (System.currentTimeMillis() - load2));
}
private static void single(String text) {
Pattern pattern = Pattern.compile("\\b(todo|fixme)\\b.*");
for (int i = 0; i < 20; i++) {
pattern.matcher(text).find();
}
}
private static void two(String text) {
Pattern todo = Pattern.compile("\\btodo\\b.*");
Pattern fixme = Pattern.compile("\\bfixme\\b.*");
for (int i = 0; i < 20; i++) {
fixme.matcher(text).find();
todo.matcher(text).find();
}
}
}
@ignatov
Copy link
Author

ignatov commented Mar 31, 2019

composite 3120
separate 6301

@ignatov
Copy link
Author

ignatov commented Mar 31, 2019

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment