Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Created December 13, 2021 12:32
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 brianmfear/99bb6712b7447b5046c8c1d257d27561 to your computer and use it in GitHub Desktop.
Save brianmfear/99bb6712b7447b5046c8c1d257d27561 to your computer and use it in GitHub Desktop.
// Randomly select A-Z, a-z, 0-9
String randomStringAllEnhanced(Integer count) {
Integer[] chars = new Integer[0],
offsets = new Integer[] { 48, 65, 97 },
mods = new Integer[] { 10, 26, 26 };
while(count > chars.size()) {
Integer rnd = Math.abs(Crypto.getRandomInteger()), cat = Math.mod(rnd, 3), seed = Math.mod(rnd, mods[cat]);
chars.add(seed+offsets[cat]);
}
return String.fromCharArray(chars);
}
// Configurable values to see the effects
Integer limitValue = 1000;
Integer searchValue = limitValue/10;
String[] searchwords = new String[0];
String[] allwords = new String[0];
// Create random words
for(Integer i = 0; i < searchValue; i++) {
searchwords.add(randomStringAllEnhanced(10));
}
for(Integer i = 0; i < limitValue; i++) {
allWords.add(randomStringAllEnhanced(10));
}
// Make sure some searchWord values are in allWords
for(Integer i = 0; i < searchValue; i++) {
allWords[(Math.random()*limitValue).intValue()] = searchWords[(Math.random()*searchValue).intValue()];
}
// Nice, long | separated list of values for regexp
String searchTerm = String.join(searchwords,'|');
Long stop1 = DateTime.now().getTime();
Pattern p = Pattern.compile(searchTerm);
for(String searchString: allwords) {
Boolean m = p.matcher(searchString).find();
}
Long stop2 = DateTime.now().getTime();
for(String searchString: allwords) {
// Search each term for each possible word, some chance of matching
for(String containsSearchTerm: searchWords) {
Boolean s = searchString.contains(containsSearchTerm);
if(s) { // We found a match
break;
}
}
}
Long stop3 = DateTime.now().getTime();
System.debug('Substring: ' + (stop3-stop2) + 'ms');
System.debug('Regexp: ' +(stop2-stop1)+'ms');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment