Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Timing {
- /** Return a tuple, where the first part is the milliseconds
- * taken and the second part is the result of the block
- * execution. */
- def time[R](block: =>R): (Long, R) = {
- val start = System.currentTimeMillis
- val result = block
- (System.currentTimeMillis - start, result)
- }
- /** Execute the block and pass the time taken to the handler. */
- def time[R](block: =>R, handler: Long=>Any): R = {
- val (ms, result) = time(block)
- handler(ms)
- result
- }
- /** Execute the command and print the time taken. */
- def printTime[R](block: =>R) {
- time(block, println)
- }
- /** A class to store timing statistics. */
- class Stats(val total: Long, val max: Long, val min: Long, val avg: Long) {
- override def toString =
- "total: " + total + "ms, min: " + min +
- "ms, max: " + max + "ms, avg: " + avg + "ms"
- }
- /** Execute the blocks and report statistics about the time taken. */
- def speedTest(executions: List[() => Any]) {
- val times = executions.map(e => time({ e () })._1)
- val total = times.reduce(_+_)
- val max = times.reduce(math.max(_, _))
- val min = times.reduce(math.min(_, _))
- val avg = total / executions.size
- new Stats(total, max, min, avg)
- }
- }
Add Comment
Please, Sign In to add comment