I’m using the metrics-scala library, and can’t understand why the two calls below behave completely differently
// method 1
writeTimer.time(() => {
// expensive operation
})
// method 2
writeTimer.time {
// expensive operation
}
In the method 1 case, the expensive operation is never called, whereas in method 2, it is.
writeTimer is an instance of com.yammer.metrics.Timer, where the time method is declared as:
/**
* Runs f, recording its duration, and returns the result of f.
*/
def time[A](f: => A): A
I just resolved a bug in my code where I had to use method 2 to get it to work.
It looks to me like in the first case, you are timing the operation of declaring a function that computes the expensive operation, and in the second case you are actually timing the expensive operation itself.