我有一个文件与600正则表达式模式的列表,大多数被执行,以找到一个特定的id为一个网站。
示例:
regex/www\.effectiveperformanceformat\.com/5
regex/bam-cell\.nr-data\.net/5
regex/advgoogle\.com/5
regex/googleapi\.club/5
regex/doubleclickbygoogle\.com/5
regex/googlerank\.info/5
regex/google-pr7\.de/5
regex/usemarketings\.com/5
regex/google-rank\.org/5
regex/googleanalytcs\.com/5
regex/xml\.trafficmoose\.com/5
regex/265\.com/5
regex/app-measurement\.com/5
regex/loftsbaacad\.com/5
regex/toldmeflex\.com/5
regex/r\.baresi\.xyz/5
regex/molodgytot\.biz/5
regex/ec\.walkme\.com/5
regex/px\.ads\.linkedin\.com/5
regex/hinisanex\.biz/5
regex/buysellads\.com/5
regex/buysellads\.net/5
regex/servedby-buysellads\.com/5
regex/carbonads\.(net|com)/5
regex/oulddev\.biz/5
regex/click\.hoolig\.app/5
regex/engine\.blacraft\.com/5
regex/mc\.yandex\.ru/5
regex/ads\.gaming1\.com/5
regex/adform\.net/5
regex/luzulabeguile\.com/5
regex/ficanportio\.biz/5
regex/hidelen\.com/5
regex/earchmess\.fun/5
regex/acrvclk\.com/5
regex/track\.wg-aff\.com/5
regex/thumb\.tapecontent\.net/5
regex/betgorebysson\.club/5
regex/in-page-push\.com/5
regex/itphanpytor\.club/5
regex/mktoresp\.com/5
regex/xid\.i-mobile\.co\.jp/5
regex/ads\.tremorhub\.com/5
目前我用的是这样的
for _, line := range file {
l := line
data := strings.Split(l, "/")
if data[0] == "regex" {
match, _ := regexp.MatchString(``+data[1]+``, website)
if match {
id, _ = strconv.Atoi(data[2])
}
}
}
这是工作,但我想知道是否有一个更优化的方式来做到这一点。因为,如果网站匹配的regex在顶部,伟大的,但如果不是,我需要intenered循环一遍又一遍,直到找到它。
有人能帮我改进吗?
顺祝商祺
1条答案
按热度按时间fbcarpbf1#
为了减少时间,您可以缓存regexp。
init
方法负责regexp缓存。它将加载Map中所有带有相对索引的regexp(它将加载仅用于基准测试的测试数据)。那么你有2个方法:
Precomputed
:使用缓存的regexp的MapNonPrecomputed
:复制-〉粘贴代码片段正如您所看到的,
NonPrecomputed
方法能够执行63次执行,Precomputed
能够执行10000次执行。正如您所看到的,NonPrecomputed
方法分配了大约67 MB,而Precomputed
方法没有分配(由于初始缓存)