The unexplained duration comes from the REPL calling toString
on the iterator returned from findAllIn
. This in turn calls Regex.MatchIterator#hasNext
, which triggers the search.
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