Created
January 4, 2017 21:18
-
-
Save MarkCLewis/dee8310e90d45dbe246901f3544b8894 to your computer and use it in GitHub Desktop.
This shows two different timing applications that I used for timing some N-body simulations in Scala.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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