Skip to content

Instantly share code, notes, and snippets.

@aya-eiya
Created July 1, 2013 13:29
Show Gist options
  • Save aya-eiya/5900765 to your computer and use it in GitHub Desktop.
Save aya-eiya/5900765 to your computer and use it in GitHub Desktop.
ちょっとしたことがGroovy「正規表現の部分で遅くなってたのを検証」 ref: http://qiita.com/aya_eiya/items/cd49e5a8a70149947a74
import java.util.regex.Matcher
import java.util.regex.Pattern
def stopWatch = {c->
s = System.nanoTime()
c()
"${(System.nanoTime() - s) / 1000} [microsec]"
}
Pattern slowp = Pattern.compile(/[^。「」]{30,}/)
def slowMatch = {target ->
def r
println "slow=" + stopWatch {
r = slowp.matcher(target).find()
}
r
}
Pattern fastp = Pattern.compile(/[^。「」]{30}/)
def fastMatch = {target ->
def r
println "fast=" + stopWatch {
r = fastp.matcher(target).find()
}
r
}
[
"0123456789",
"0123456789"*2,
"0123456789"*3,
"0123456789"*3+"。", // 否定対象があるが範囲外
"0123456789"*2.9+"。",
"012345678。"*500, // 否定対象がある長い文字列 ← 速くはならないはず
"0123456789"*500+"。", // 否定対象があるが範囲外
"0123456789"*500+"○", // 否定対象がない長い文字列
].each{target ->
println "== taget.lenght() : ${target.length()} =="
assert slowMatch(target) == fastMatch(target)
}
""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment