Skip to content

Instantly share code, notes, and snippets.

@gourlaysama
Created June 25, 2014 12:41
Show Gist options
  • Save gourlaysama/0817698e042caa932a0f to your computer and use it in GitHub Desktop.
Save gourlaysama/0817698e042caa932a0f to your computer and use it in GitHub Desktop.
small benchmark for SI-7710 / SI-8542 in scala-parser-combinators
package test
import scala.util.parsing.combinator.RegexParsers
import java.io.StringReader
object SI8542_ParserCombinatorBenchmark extends App with RegexParsers {
lazy val line = """(?m)^.*$""".r ^^ { _ => 0 }
lazy val parser = rep(line) ^^ { _ => 1 }
def inputGen: (Int => String) = (i: Int) => ("100:59:59,333\n") * i
def parseAsString = (s: String) => parseAll(parser, s)
def parseAsReader = (s: String) => parseAll(parser, new StringReader(s))
parseAsReader(inputGen(1000))
parseAsString(inputGen(1000))
benchmark("parseAll(new StringReader(String))", inputGen, parseAsReader)
println("===")
benchmark("parseAll(String)", inputGen, parseAsString)
def benchmark(name: String, inputGen: Int => String, f: String => ParseResult[Any]): Unit = {
println(name)
def now = System.currentTimeMillis
val quantities = Seq(1000, 5000, 10000, 50000, 100000, 500000)
quantities.foreach { q =>
val concatInput = inputGen(q)
val startTime = now
val parseRes = f(concatInput)
assert(parseRes.successful, parseRes)
val timeTaken = now - startTime
println(s"For $q items: $timeTaken ms")
}
}
}
Before the fix:
parseAll(new StringReader(String))
For 1000 items: 36 ms
For 5000 items: 119 ms
For 10000 items: 143 ms
For 50000 items: 769 ms
For 100000 items: 333 ms
For 500000 items: 6086 ms
===
parseAll(String)
For 1000 items: 73 ms
For 5000 items: 591 ms
For 10000 items: 1611 ms
For 50000 items: 33832 ms
^C⏎
After the fix:
parseAll(new StringReader(String))
For 1000 items: 58 ms
For 5000 items: 152 ms
For 10000 items: 184 ms
For 50000 items: 542 ms
For 100000 items: 305 ms
For 500000 items: 4377 ms
===
parseAll(String)
For 1000 items: 3 ms
For 5000 items: 13 ms
For 10000 items: 42 ms
For 50000 items: 154 ms
For 100000 items: 244 ms
For 500000 items: 463 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment