1. Jean-Marie Gaillourdet
  2. chronos

Commits

Jean-Marie Gaillourdet  committed c2eadab

initial version

  • Participants
  • Branches default

Comments (0)

Files changed (5)

File .hgignore

View file
  • Ignore whitespace
+target
+.idea

File build.sbt

View file
  • Ignore whitespace
+name := "chronos"
+
+organization := "gaillourdet.net"
+
+version := "0.1-SNAPSHOT"
+
+scalaVersion := "2.10.2"
+
+scalacOptions += "-optimise"
+
+libraryDependencies += "org.scalatest" % "scalatest_2.10" % "1.9.1" % "test"
+
+libraryDependencies += "com.github.wookietreiber" %% "scala-chart" % "latest.integration"

File project/build.properties

View file
  • Ignore whitespace
+sbt.version=0.12.3

File src/main/scala/Chronos.scala

View file
  • Ignore whitespace
+
+class Chronos {
+
+
+  def measureClockResolution = {
+    val times: Array[Long] = Array.fill(10000000){System.nanoTime()}
+    val diffs = for {
+      idx <- 0 until times.length - 1
+    } yield times(idx+1) - times(idx)
+    val mean = diffs.sum / diffs.length
+    mean
+  }
+  def measureClockResolution2(iterations: Int): (Long, Int, Int) = {
+    var numNonZeroMeasurements = 0
+    var numZeroMeasurements = 0
+    var sumTimeDiffs: Long = 0
+    var lastTime = System.nanoTime()
+    do {
+      val now = System.nanoTime()
+      val diff = now - lastTime
+      if (diff == 0) {
+        numZeroMeasurements += 1
+      } else {
+        numNonZeroMeasurements += 1
+        sumTimeDiffs += diff
+      }
+      lastTime = now
+    } while (numNonZeroMeasurements + numZeroMeasurements < iterations)
+    (sumTimeDiffs / numNonZeroMeasurements, numNonZeroMeasurements, numZeroMeasurements)
+  }
+
+  def measureClockResolution3(iterations: Int): Iterable[Long] = {
+    val times = Array.fill(iterations)(System.nanoTime())
+    for {
+      (t1,t2) <- times.toIterable zip times.toIterable.tail
+    } yield t2 - t1
+  }
+
+  // Using an argument in the measured block seems to be necessary in order
+  // to avoid optimizing the measured block away.
+  def timeExecution[A,B](argument: A)(block: A => B): Long = {
+    val startTime = System.nanoTime()
+    try {
+      for (idx <- 1 to 1000000) {
+        val _ = block(argument)
+      }
+    } catch {
+      case e: Error => throw e
+      case e: Exception =>
+    }
+    val endTime = System.nanoTime()
+    endTime - startTime
+  }
+
+}

File src/main/scala/Example1.scala

View file
  • Ignore whitespace
+import scalax.chart._
+import scalax.chart.Charting._
+
+object Example1 extends App {
+
+  val chronos = new Chronos
+
+  def experiment =
+//    chronos.timeExecution2[Int, Int](17) { x =>
+    chronos.timeExecution[Int, Int](17) { x =>
+//      47 ^ x
+      0
+    }
+
+//  println(chronos.measureClockResolution2(10000000))
+
+  val experimentalResults = chronos.measureClockResolution3(1000000)
+  println("measurements finished")
+  val indexed = experimentalResults.zipWithIndex.toVector
+  println("indexed")
+  (indexed groupBy (_._1) mapValues (_.length)).toList sortBy (_._1) foreach println
+  val chart = XYLineChart(indexed.toXYSeriesCollection("nanoTimes"), title = "time")
+  chart.show
+//  val timings = for (idx <- 1 to 1000) yield (idx, experiment)
+
+//  val chart = XYLineChart(timings.toXYSeriesCollection("measured running times"), title = "my dataset")
+//  chart.show
+
+
+}