Skip to content

Instantly share code, notes, and snippets.

@bxem44
Forked from kandelvijaya/PrecisionTimer.swift
Created March 8, 2019 09:48
Show Gist options
  • Save bxem44/6b32bde5a7d538193a3f99a3af9499bf to your computer and use it in GitHub Desktop.
Save bxem44/6b32bde5a7d538193a3f99a3af9499bf to your computer and use it in GitHub Desktop.
Precision Timing in iOS/OSX/Swift
//: Playground - noun: a place where code can play
import UIKit
//Most precise time keeper
// for more information on the benchmarks go to www.kandelvijaya.com
func timeBlockWithMach(_ block: () -> Void) -> TimeInterval {
var info = mach_timebase_info()
guard mach_timebase_info(&info) == KERN_SUCCESS else { return -1 }
let start = mach_absolute_time()
block()
let end = mach_absolute_time()
let elapsed = end - start
let nanos = elapsed * UInt64(info.numer) / UInt64(info.denom)
return TimeInterval(nanos) / TimeInterval(NSEC_PER_SEC)
}
func test() {
let rtDate = timeBlockWithMach{
NSDate().timeIntervalSince1970
}
let rtMedia = timeBlockWithMach {
CACurrentMediaTime()
}
let rtAbsolute = timeBlockWithMach {
CFAbsoluteTimeGetCurrent()
}
var time = timeval()
let rtTimeOfDay = timeBlockWithMach {
gettimeofday(&time, nil)
}
let rtTimeWithMach = timeBlockWithMach {
mach_absolute_time()
}
let rtTimeWithProcessInfo = timeBlockWithMach {
ProcessInfo.processInfo.systemUptime
}
}
test()
// MARK: - Other timing functions
func timeBlockWithDateTime(_ block: () -> Void) -> TimeInterval {
let start = NSDate().timeIntervalSince1970
block()
let end = NSDate().timeIntervalSince1970
return end - start
}
func timeBlockWithCAMediaTiming(_ block: () -> Void) -> TimeInterval {
let start = CACurrentMediaTime()
block()
let end = CACurrentMediaTime()
return end - start
}
func timeBlockWithCFTime(_ block: () -> Void) -> TimeInterval {
let start = CFAbsoluteTimeGetCurrent()
block()
let end = CFAbsoluteTimeGetCurrent()
return end - start
}
func timeBlockWithGetTimeOfDay(_ block: () -> Void) -> TimeInterval {
var start = timeval()
gettimeofday(&start, nil)
block()
var end = timeval()
gettimeofday(&end, nil)
return Double(start.tv_usec - end.tv_usec)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment