1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| class Benchmark {
// 開始時刻を保存する変数
var startTime: Date
var key: String
// 処理開始
init(_ key: String) {
self.startTime = Date()
self.key = key
}
// 処理終了
func finish() {
let elapsed = Date().timeIntervalSince(self.startTime) as Double
let formatedElapsed = String(format: "%.3f", elapsed)
print("Benchmark: \(key), Elasped time: \(formatedElapsed)(s)")
}
// 処理をブロックで受け取る
class func measure(key: String, block: () -> ()) {
let benchmark = Benchmark(key)
block()
benchmark.finish()
}
}
|