Created
June 14, 2011 09:23
-
-
Save xnrghzjh/1024575 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 { | |
def lapTimes = [] | |
def startTime | |
def stopTime | |
def cur = { System.currentTimeMillis() } | |
def start = { startTime = cur() } | |
def stop = { stopTime = cur() } | |
def elapsedTime = { stopTime - startTime } | |
def elapsedLapTime = { lapTimes[it].time - startTime } | |
def lap = { | |
// lapTimes.add(new LapTime(name: it, time: cur())) // これがダメな理由がまだ勉強不足で分からない | |
def l = new LapTime() | |
l.name = it | |
l.time = cur() | |
lapTimes.add(l) | |
lapTimes.size() | |
} | |
class LapTime { | |
def name | |
def time | |
} | |
} | |
// テスト用メソッド | |
Long.metaClass.toString = {"${delegate} ミリ秒経過"} | |
// Exec | |
watch = new StopWatch() | |
sleepTime = 100 | |
println "■ 計測 $sleepTime 秒スリープ" | |
watch.start() | |
sleep(sleepTime) | |
watch.stop() | |
println " ${watch.elapsedTime().toString()}" | |
println "■ ラップ計測 $sleepTime 秒スリープ" | |
watch.start() | |
sleep(sleepTime) | |
watch.lap('1st Lap') | |
sleep(sleepTime) | |
watch.lap('2nd Lap') | |
sleep(sleepTime) | |
watch.stop() | |
println " ALL\t: ${watch.elapsedTime().toString()}" | |
watch.lapTimes.eachWithIndex() { it, index -> | |
println " ${it.name}\t: ${watch.elapsedLapTime(index).toString()}" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment