正则表达式 guava_带有正则表达式模式的Google Guava Cache
正則表達式 guava
最近我看到了一個關于Google Guava的不錯的介紹 ,我們在我們的項目中得出結論,使用它的緩存功能真的很有趣。 讓我們看一下regexp Pattern類及其編譯功能 。 在代碼中我們經常可以看到,每次使用正則表達式時,程序員都會使用相同的參數重復調用上述Pattern.compile()函數,從而一次又一次地編譯相同的正則表達式。 但是,可以做的是緩存此類編譯的結果–讓我們看一下RegexpUtils實用程序類:
RegexpUtils.java
package pl.grzejszczak.marcin.guava.cache.utils;import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache;import java.util.concurrent.ExecutionException; import java.util.regex.Matcher; import java.util.regex.Pattern;import static java.lang.String.format;public final class RegexpUtils {private RegexpUtils() {throw new UnsupportedOperationException("RegexpUtils is a utility class - don't instantiate it!");}private static final LoadingCache<String, Pattern> COMPILED_PATTERNS =CacheBuilder.newBuilder().build(new CacheLoader<String, Pattern>() {@Overridepublic Pattern load(String regexp) throws Exception {return Pattern.compile(regexp);}});public static Pattern getPattern(String regexp) {try {return COMPILED_PATTERNS.get(regexp);} catch (ExecutionException e) {throw new RuntimeException(format("Error when getting a pattern [%s] from cache", regexp), e);}}public static boolean matches(String stringToCheck, String regexp) {return doGetMatcher(stringToCheck, regexp).matches();}public static Matcher getMatcher(String stringToCheck, String regexp) {return doGetMatcher(stringToCheck, regexp);}private static Matcher doGetMatcher(String stringToCheck, String regexp) {Pattern pattern = getPattern(regexp);return pattern.matcher(stringToCheck);}}如您所見,如果沒有找到,則使用帶有CacheBuilder的Guava的LoadingCache來填充具有新編譯模式的緩存。 由于緩存已編譯的模式(如果已經進行了編譯),將不會再次重復(在我們的情況下,因為我們沒有任何到期設置)。 現在一個簡單的測試
GuavaCache.java
package pl.grzejszczak.marcin.guava.cache;import com.google.common.base.Stopwatch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.grzejszczak.marcin.guava.cache.utils.RegexpUtils;import java.util.regex.Pattern;import static java.lang.String.format;public class GuavaCache {private static final Logger LOGGER = LoggerFactory.getLogger(GuavaCache.class);public static final String STRING_TO_MATCH = "something";public static void main(String[] args) {runTestForManualCompilationAndOneUsingCache(1);runTestForManualCompilationAndOneUsingCache(10);runTestForManualCompilationAndOneUsingCache(100);runTestForManualCompilationAndOneUsingCache(1000);runTestForManualCompilationAndOneUsingCache(10000);runTestForManualCompilationAndOneUsingCache(100000);runTestForManualCompilationAndOneUsingCache(1000000);}private static void runTestForManualCompilationAndOneUsingCache(int firstNoOfRepetitions) {repeatManualCompilation(firstNoOfRepetitions);repeatCompilationWithCache(firstNoOfRepetitions);}private static void repeatManualCompilation(int noOfRepetitions) {Stopwatch stopwatch = new Stopwatch().start();compileAndMatchPatternManually(noOfRepetitions);LOGGER.debug(format("Time needed to compile and check regexp expression [%d] ms, no of iterations [%d]", stopwatch.elapsedMillis(), noOfRepetitions));}private static void repeatCompilationWithCache(int noOfRepetitions) {Stopwatch stopwatch = new Stopwatch().start();compileAndMatchPatternUsingCache(noOfRepetitions);LOGGER.debug(format("Time needed to compile and check regexp expression using Cache [%d] ms, no of iterations [%d]", stopwatch.elapsedMillis(), noOfRepetitions));}private static void compileAndMatchPatternManually(int limit) {for (int i = 0; i < limit; i++) {Pattern.compile("something").matcher(STRING_TO_MATCH).matches();Pattern.compile("something1").matcher(STRING_TO_MATCH).matches();Pattern.compile("something2").matcher(STRING_TO_MATCH).matches();Pattern.compile("something3").matcher(STRING_TO_MATCH).matches();Pattern.compile("something4").matcher(STRING_TO_MATCH).matches();Pattern.compile("something5").matcher(STRING_TO_MATCH).matches();Pattern.compile("something6").matcher(STRING_TO_MATCH).matches();Pattern.compile("something7").matcher(STRING_TO_MATCH).matches();Pattern.compile("something8").matcher(STRING_TO_MATCH).matches();Pattern.compile("something9").matcher(STRING_TO_MATCH).matches();}}private static void compileAndMatchPatternUsingCache(int limit) {for (int i = 0; i < limit; i++) {RegexpUtils.matches(STRING_TO_MATCH, "something");RegexpUtils.matches(STRING_TO_MATCH, "something1");RegexpUtils.matches(STRING_TO_MATCH, "something2");RegexpUtils.matches(STRING_TO_MATCH, "something3");RegexpUtils.matches(STRING_TO_MATCH, "something4");RegexpUtils.matches(STRING_TO_MATCH, "something5");RegexpUtils.matches(STRING_TO_MATCH, "something6");RegexpUtils.matches(STRING_TO_MATCH, "something7");RegexpUtils.matches(STRING_TO_MATCH, "something8");RegexpUtils.matches(STRING_TO_MATCH, "something9");}}}我們正在運行一系列測試,并檢查它們的執行時間。 請注意,由于應用程序不是獨立運行的,因此這些測試的結果并不精確,因此許多條件都可能影響執行時間。 我們有興趣顯示一定程度的問題,而不是顯示準確的執行時間。 對于給定的迭代次數(1,10,100,1000,10000,100000,1000000),我們要么編譯10個正則表達式,要么使用Guava的緩存檢索已編譯的Pattern,然后將它們與要匹配的字符串進行匹配。 這些是日志:
pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [1] ms, no of iterations [1] pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [35] ms, no of iterations [1] pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [1] ms, no of iterations [10] pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [0] ms, no of iterations [10] pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [8] ms, no of iterations [100] pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [3] ms, no of iterations [100] pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [10] ms, no of iterations [1000] pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [10] ms, no of iterations [1000] pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [83] ms, no of iterations [10000] pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [33] ms, no of iterations [10000] pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [800] ms, no of iterations [100000] pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [279] ms, no of iterations [100000] pl.grzejszczak.marcin.guava.cache.GuavaCache:34 Time needed to compile and check regexp expression [7562] ms, no of iterations [1000000] pl.grzejszczak.marcin.guava.cache.GuavaCache:40 Time needed to compile and check regexp expression using Cache [3067] ms, no of iterations [1000000] 您可以在Guava / Cache目錄下的此處找到源,或轉到URL https://bitbucket.org/gregorin1987/too-much-coding/src
翻譯自: https://www.javacodegeeks.com/2013/04/google-guava-cache-with-regular-expression-patterns.html
正則表達式 guava
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的正则表达式 guava_带有正则表达式模式的Google Guava Cache的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 运城手机靓号交易中心(运城移动靓号选购)
- 下一篇: 大脑控制电脑(大脑控制电脑的软件)