Created
August 15, 2009 15:19
-
-
Save stepancheg/168376 to your computer and use it in GitHub Desktop.
Junk — dummy profiler
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
package scala.tools.nsc | |
object Junk { | |
def currentTimeMicros = System.nanoTime / 1000 | |
case class Current(stack: List[String], start: Long) | |
var current: Option[Current] = None | |
val map = new scala.collection.mutable.HashMap[String, Long] | |
private def updateCurrent() { | |
if (current.isDefined) { | |
val now = currentTimeMicros | |
val d = currentTimeMicros - current.get.start | |
val name = current.get.stack.head | |
map.get(name) match { | |
case Some(micros) => map.update(name, micros + d) | |
case None => map.update(name, d) | |
} | |
} | |
} | |
private val enable = true | |
def now(name: String) = if (enable) { | |
updateCurrent() | |
current = Some(Current(name :: (current.map(_.stack).getOrElse(Nil)), currentTimeMicros)) | |
} | |
def pop() = if (enable) { | |
require(current.isDefined) | |
updateCurrent() | |
current.get.stack match { | |
case _ :: Nil => current = None | |
case first :: rest => current = Some(Current(rest, currentTimeMicros)) | |
} | |
} | |
def change(name: String) = { | |
pop() | |
now(name) | |
} | |
def enter[T](name: String)(t: => T) = | |
try { | |
now(name) | |
t | |
} finally { | |
pop() | |
} | |
def printStats() = if (enable) { | |
require(current.isEmpty) | |
println("stats") | |
for ((name, micros) <- scala.util.Sorting.stableSort(map.toList, (e: (String, Long)) => e._2)) | |
println(name + ": " + (micros / 1000)) | |
} | |
} | |
// vim: set ts=4 sw=4 et: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment