我正在使用Scala来衡量java的正则表达式引擎的性能.下面的正则表达式大约在3秒内执行,但我无法使用System.currentTimeMillis进行测量.(最后一个表达式返回0)
scala> val b = System.currentTimeMillis; val v = new Regex("(x+)+y").findAllIn("x"*25); b-System.currentTimeMillis
b: Long = 1330787275629
v: scala.util.matching.Regex.MatchIterator = empty iterator
res18: Long = 0
Run Code Online (Sandbox Code Playgroud)
您现在为什么最后返回的值为0,而不是scala在执行regexp时花费的ms量?
ret*_*nym 81
原因不明的持续时间来自REPL调用toString返回的迭代器findAllIn.这反过来调用Regex.MatchIterator#hasNext,触发搜索.
scala> def time[A](a: => A) = {
| val now = System.nanoTime
| val result = a
| val micros = (System.nanoTime - now) / 1000
| println("%d microseconds".format(micros))
| result
| }
time: [A](a: => A)A
scala> :power
** Power User mode enabled - BEEP WHIR GYVE **
** :phase has been set to 'typer'. **
** scala.tools.nsc._ has been imported **
** global._, definitions._ also imported **
** Try :help, :vals, power.<tab> **
scala> :wrap time
Set wrapper to 'time'
scala> new Regex("(x+)+y").findAllIn("x"*25).toString
3000737 microseconds
res19: String = empty iterator
scala> {new Regex("(x+)+y").findAllIn("x"*25); 0}
582 microseconds
res20: Int = 0
Run Code Online (Sandbox Code Playgroud)
kir*_*uku 28
def time[A](f: => A) = {
val s = System.nanoTime
val ret = f
println("time: "+(System.nanoTime-s)/1e6+"ms")
ret
}
Run Code Online (Sandbox Code Playgroud)
使用它:
scala> time { 10*2 }
time: 0.054212ms
res1: Int = 20
Run Code Online (Sandbox Code Playgroud)
这很有意思!我添加了一个println("start")和"end"周围创建正则表达式并运行代码行-这版画
start
end
Run Code Online (Sandbox Code Playgroud)
然后在打印输出的其余部分之前暂停约三秒钟.
所以看起来正在发生的是正在创建的正则表达式,但是直到toString被调用才能将其输出到控制台.要使测试起作用,toString请在计算花费的时间之前添加手动调用.
scala> val b = System.currentTimeMillis; val v = new scala.util.matching.Regex("(x+)+y").findAllIn("x"*25); v.toString; System.currentTimeMillis-b
b: Long = 1330789547209
v: scala.util.matching.Regex.MatchIterator = empty iterator
res14: Long = 4881
Run Code Online (Sandbox Code Playgroud)
它应该是System.currentTimeMillis-b而不是相反...
| 归档时间: |
|
| 查看次数: |
25632 次 |
| 最近记录: |