Skip to content

Instantly share code, notes, and snippets.

@jbn
Created November 11, 2015 22:15
Show Gist options
  • Save jbn/eeebd9a2024168e564ee to your computer and use it in GitHub Desktop.
Save jbn/eeebd9a2024168e564ee to your computer and use it in GitHub Desktop.
A script to test the time-expense of scala.math.ulp
import scala.math.ulp
/**
* ThirdWay calls `ulp` occasionally. This script tries to assess costs.
*/
def timeIt[R](f: => R): Long = {
val start = System.currentTimeMillis
f
System.currentTimeMillis - start
}
val n = 1000000
val timings = (0 until n).map { i =>
val x = i.toDouble
timeIt { ulp(x) }
}
val μ = timings.sum / n.toDouble
// # => On my MBP mid-2012
println(s"Average timing: $μ ms") // # => 6.9E-5 ms
println(s"Ops per second: ${1.0/μ*1000}") // # => 1.4492753623188406E7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment