Use nanotime for profiling and use more iterations

This doesn't make the benchmarks all accurate or anything
but maybe makes them a little less likely to be way off.
This commit is contained in:
Havoc Pennington 2014-10-10 20:47:28 -04:00
parent 77972fefc3
commit 891a2cde97

View File

@ -4,20 +4,39 @@
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigException
import java.util.concurrent.TimeUnit
import scala.annotation.tailrec
object Util {
def time(body: () => Unit, iterations: Int): Double = {
@tailrec
def timeHelper(body: () => Unit, iterations: Int, retried: Boolean): Double = {
// warm up
for (i <- 1 to Math.max(20, iterations / 10)) {
body()
}
val start = System.currentTimeMillis()
val start = System.nanoTime()
for (i <- 1 to iterations) {
body()
}
val end = System.currentTimeMillis()
(end - start).toDouble / iterations
val end = System.nanoTime()
val elapsed = end - start
val nanosInMillisecond = 1000000L
if (elapsed < (1000 * nanosInMillisecond)) {
System.err.println(s"Total time for $iterations was less than a second; trying with more iterations")
timeHelper(body, iterations * 10, true)
} else {
if (retried)
System.out.println(s"with $iterations we got a long enough sample (${elapsed.toDouble / nanosInMillisecond}ms)")
(elapsed.toDouble / iterations) / nanosInMillisecond
}
}
def time(body: () => Unit, iterations: Int): Double = {
timeHelper(body, iterations, false)
}
def loop(args: Seq[String], body: () => Unit) {
@ -38,7 +57,7 @@ object FileLoad extends App {
}
}
val ms = Util.time(task, 100)
val ms = Util.time(task, 4000)
println("file load: " + ms + "ms")
Util.loop(args, task)
@ -54,7 +73,7 @@ object Resolve extends App {
}
}
val ms = Util.time(task, 10000)
val ms = Util.time(task, 3000000)
println("resolve: " + ms + "ms")
Util.loop(args, task)
@ -69,7 +88,7 @@ object GetExistingPath extends App {
}
}
val ms = Util.time(task, 100000)
val ms = Util.time(task, 2000000)
println("GetExistingPath: " + ms + "ms")
Util.loop(args, task)
@ -84,7 +103,7 @@ object HasPathOnMissing extends App {
}
}
val ms = Util.time(task, 100000)
val ms = Util.time(task, 20000000)
println("HasPathOnMissing: " + ms + "ms")
Util.loop(args, task)
@ -100,7 +119,7 @@ object CatchExceptionOnMissing extends App {
}
}
val ms = Util.time(task, 100000)
val ms = Util.time(task, 3000000)
println("CatchExceptionOnMissing: " + ms + "ms")
Util.loop(args, task)