Skip to content

Instantly share code, notes, and snippets.

@nobeans
Last active March 2, 2018 07:35
Show Gist options
  • Save nobeans/5167629 to your computer and use it in GitHub Desktop.
Save nobeans/5167629 to your computer and use it in GitHub Desktop.
class StopWatch {
private timeRecoreds = [:].withDefault { 0.0 }
def withTimeRecording(keyword, clos) {
def beginTime = System.currentTimeMillis()
try {
return clos.call()
} finally {
timeRecoreds[keyword] += System.currentTimeMillis() - beginTime
}
}
def printResult() {
timeRecoreds.each { keyword, time ->
println "timeResult: ${keyword}: ${time / 1000.0} (sec)"
}
}
}
def watch = new StopWatch()
watch.withTimeRecording("total") {
999.times { index ->
watch.withTimeRecording("A") {
// do something A
sleep 1
}
watch.withTimeRecording("B") {
// do something B
sleep 2
}
}
}
watch.printResult()
// timeResult: A: 1.312 (sec)
// timeResult: B: 2.398 (sec)
// timeResult: total: 3.877 (sec)
@dtissen
Copy link

dtissen commented Mar 2, 2018

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment