Create a gist now

Instantly share code, notes, and snippets.

This shows two different timing applications that I used for timing some N-body simulations in Scala.
object MainTiming extends App {
def timeCode[T](warmups: Int, timeRuns: Int)(body: => T): Seq[Double] = {
for(_ <- 1 to warmups) body
for(_ <- 1 to timeRuns) yield {
val start = System.nanoTime()
body
(System.nanoTime()-start)*1e-9
}
}
def printTimeInfo(times: Seq[Double]): Unit = {
val mean = times.sum/times.length
val rms = math.sqrt(times.map(x => (x-mean)*(x-mean)).sum/times.length)
println(s"mean = $mean, rms = $rms")
}
val numBodies = 1000
val dt = 0.01
NBodyValClass.initArrays(numBodies, dt)
println("Value Type:")
printTimeInfo(timeCode(5,20){ NBodyValClass.forSim(100) })
val mutSim = new NBodyMutableClass(numBodies, dt)
println("Mutable Class:")
printTimeInfo(timeCode(5,20){ mutSim.forSim(100) })
val immutSim = new NBodyImmutableClass(numBodies, dt)
println("Immutable Class:")
printTimeInfo(timeCode(5,20){ immutSim.forSim(100) })
val funcBodies = NBodyFunctional.initBodies(numBodies)
println("Functional 1:")
printTimeInfo(timeCode(5,20){ NBodyFunctional.forSim(funcBodies,100,dt) })
println("Functional 2:")
printTimeInfo(timeCode(5,20){ NBodyFunctional.forSim2(funcBodies,100,dt) })
}
object SimpleTiming extends App {
val numBodies = 1000
val dt = 0.01
NBodyValClass.initArrays(numBodies, dt)
val start = System.nanoTime()
NBodyValClass.forSim(1000)
println((System.nanoTime()-start)*1e-9)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment