Guest User

Untitled

a guest
Jan 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. object Timing {
  2. /** Return a tuple, where the first part is the milliseconds
  3. * taken and the second part is the result of the block
  4. * execution. */
  5. def time[R](block: =>R): (Long, R) = {
  6. val start = System.currentTimeMillis
  7. val result = block
  8. (System.currentTimeMillis - start, result)
  9. }
  10.  
  11. /** Execute the block and pass the time taken to the handler. */
  12. def time[R](block: =>R, handler: Long=>Any): R = {
  13. val (ms, result) = time(block)
  14. handler(ms)
  15. result
  16. }
  17.  
  18. /** Execute the command and print the time taken. */
  19. def printTime[R](block: =>R) {
  20. time(block, println)
  21. }
  22.  
  23. /** A class to store timing statistics. */
  24. class Stats(val total: Long, val max: Long, val min: Long, val avg: Long) {
  25. override def toString =
  26. "total: " + total + "ms, min: " + min +
  27. "ms, max: " + max + "ms, avg: " + avg + "ms"
  28. }
  29.  
  30. /** Execute the blocks and report statistics about the time taken. */
  31. def speedTest(executions: List[() => Any]) {
  32. val times = executions.map(e => time({ e () })._1)
  33.  
  34. val total = times.reduce(_+_)
  35. val max = times.reduce(math.max(_, _))
  36. val min = times.reduce(math.min(_, _))
  37. val avg = total / executions.size
  38.  
  39. new Stats(total, max, min, avg)
  40. }
  41. }
Add Comment
Please, Sign In to add comment