Last active
March 2, 2018 07:35
-
-
Save nobeans/5167629 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gbenchで十分だな...
http://nagaimasato-ja.blogspot.jp/2011/06/gbench-benchmark-annotation.html