Timer
wvlet.log.io.Timer
trait Timer extends Serializable
Timer trait measures the performance of code blocks. Extend this trait and wrap the code to measure with time(code_name){ ... }
:
class A extends Timer {
val t : TimeReport = time("performance test") {
// write any code here
block("A") {
// code A
}
block("B") {
// code B
}
}
// report elapsed time of A, B and the total running time
println(t)
t("A").average // the average of running time of code block "A" (min and max are also available)
}
Timer can take the average of repetitive executions:
class Rep extends Timer {
// Repeat 10 times the evaluation of the whole block
val t = time("repetitive evaluation", repeat=10) {
// This part will be executed 1000 x 10 times
block("A", repeat=1000) {
// code A
}
// This part will be executed 1000 x 10 times
block("B", repeat=1000) {
// code B
}
}
println(t)
// Which code is faster?
if(t("A") <= t("B"))
println("A is faster")
else
println("B is faster")
}
When measuring Scala (Java) code performances, you should take the average of execution times and reorder the code block execution orders, because JVM has JIT compiler, which optimizes the code at runtime. And also cache usage and the running state of the garbage core (GC) affects the code performance. By repeating the executions of the entire or individual blocks with the repeat
option, you can avoid such pitfalls of benchmarking.
Attributes
- Graph
-
- Supertypes
-
trait Serializableclass Objecttrait Matchableclass Any
Members list
In this article