Skip to content

Instantly share code, notes, and snippets.

@irskep
Created December 22, 2015 23:47
Show Gist options
  • Save irskep/cd6656abba6ccb0e37c0 to your computer and use it in GitHub Desktop.
Save irskep/cd6656abba6ccb0e37c0 to your computer and use it in GitHub Desktop.
// https://gist.github.com/jstn/d93c86f7bd2b6f22f0bf
/*
var b = Benchmarker()
b.start()
// do something
b.stop()
println("took \(b.milliseconds) ms")
*/
import Darwin
struct Benchmarker {
static var t = mach_timebase_info(numer: 0, denom: 0)
var startTime = UInt64(0)
var duration = UInt64(0)
var milliseconds: Double {
return Double(duration) / 1_000_000
}
init() {
if Benchmarker.t.denom == 0 {
mach_timebase_info(&Benchmarker.t)
}
}
mutating func start() {
startTime = mach_absolute_time()
}
mutating func stop() {
let delta = mach_absolute_time() - startTime
duration = (delta * UInt64(Benchmarker.t.numer)) / UInt64(Benchmarker.t.denom)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment