Skip to content

Instantly share code, notes, and snippets.

@kongtomorrow
Created August 11, 2014 18:14
Show Gist options
  • Save kongtomorrow/b7830e039e1618456281 to your computer and use it in GitHub Desktop.
Save kongtomorrow/b7830e039e1618456281 to your computer and use it in GitHub Desktop.
benchmark
func benchmark<T>(label:String, body:()->T) {
var info = mach_timebase_info_data_t(numer: 0, denom: 0)
mach_timebase_info(&info)
let tick = mach_absolute_time()
let res = body()
let tock = mach_absolute_time()
let delta = tock - tick
let nanos = delta * UInt64(info.numer) / UInt64(info.denom)
let seconds = Double(nanos) / Double(NSEC_PER_SEC)
"\(label) returned \(res) in \(seconds) seconds"***
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment