LoginSignup
0
0

More than 5 years have passed since last update.

ちょっとしたことがGroovy「正規表現の部分で遅くなってたのを検証」

Posted at

微妙に正規表現の部分で遅くなってたので、こうすりゃいんじゃね?ってのを検証してみた。

今回とくにGroovyだから便利ってとこは、ないかな。

いや、あるかな。テスト用の文字列つくるとこかな。

なお、マッチした部分を他に使う必要のないコードです。

regexp_faster.groovy
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)
}
""
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0