Skip to content

Instantly share code, notes, and snippets.

@dylan
Last active January 30, 2016 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylan/a36c6af0c82a436bdcda to your computer and use it in GitHub Desktop.
Save dylan/a36c6af0c82a436bdcda to your computer and use it in GitHub Desktop.
import Foundation
public struct Duration {
private var _nanos: UInt64
public var nanoseconds: Double {
get {
return Double(_nanos)
}
}
public var microseconds: Double {
return nanoseconds / 1e3
}
public var milliseconds: Double {
return nanoseconds / 1e6
}
public var seconds: Double {
return nanoseconds / 1e9
}
public var minutes: Double {
return nanoseconds / 60_000_000_000
}
public var hours: Double {
return minutes / 60
}
// MARK: Initializers
public init(nanos: UInt64) {
_nanos = nanos
}
public init(microseconds: Double) {
_nanos = UInt64(microseconds * 1e3)
}
public init(milliseconds: Double) {
_nanos = UInt64(milliseconds * 1e6)
}
public init(seconds: Double) {
_nanos = UInt64(seconds * 1e9)
}
public init(minutes: Double) {
_nanos = UInt64(minutes * 60_000_000_000)
}
public init(hours: Double) {
_nanos = UInt64(hours * 60_000_000_000 * 60)
}
}
// TODO: Make equatable/comparable etc.
public func +(left: Duration, right: Duration) -> Duration {
return Duration(nanos: UInt64(left.nanoseconds + right.nanoseconds))
}
public func -(left: Duration, right: Duration) -> Duration {
return Duration(nanos: UInt64(left.nanoseconds - right.nanoseconds))
}
public func /(left: Duration, right: Duration) -> Duration {
return Duration(nanos: UInt64(left.nanoseconds / right.nanoseconds))
}
public func >(left: Duration, right: Duration) -> Bool {
return left.nanoseconds > right.nanoseconds
}
public func <(left: Duration, right: Duration) -> Bool {
return left.nanoseconds < right.nanoseconds
}
public func >=(left: Duration, right: Duration) -> Bool {
return left.nanoseconds >= right.nanoseconds
}
public func ==(left: Duration, right: Duration) -> Bool {
return left.nanoseconds == right.nanoseconds
}
import Foundation
public struct MachTimer {
static var base: UInt64 = 0
private var startTime: UInt64 = 0
private var currentTime: UInt64 = 0
private var lastSample: UInt64 = 0
private var nextTime: UInt64 = 0
private var stopTime: UInt64 = 0
private var _nanos: UInt64 {
let st = stopTime == 0 ? mach_absolute_time() : stopTime
return (st - startTime) * MachTimer.base
}
public var elapsed: Duration {
return Duration(nanos: _nanos)
}
public init() {
if MachTimer.base == 0 {
var info = mach_timebase_info_data_t(numer: 0, denom: 0)
mach_timebase_info(&info)
MachTimer.base = UInt64(info.numer / info.denom)
}
}
public mutating func start() {
startTime = mach_absolute_time()
}
public mutating func stop() {
stopTime = mach_absolute_time()
}
public mutating func delta() -> Duration {
lastSample = lastSample == 0 ? _nanos : currentTime
currentTime = mach_absolute_time()
return Duration(nanos: currentTime - lastSample)
}
public mutating func now() -> Duration {
currentTime = mach_absolute_time()
return Duration(nanos: currentTime)
}
}
import Foundation
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
var timer = MachTimer()
timer.start()
var i = 0
repeat {
i++
print(timer.elapsed.milliseconds)
print(timer.delta().milliseconds)
print(timer.now().nanoseconds)
} while i < 2500
timer.stop()
timer.elapsed.nanoseconds
timer.elapsed.microseconds
timer.elapsed.milliseconds
timer.elapsed.seconds
timer.elapsed.minutes
timer.elapsed.hours
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment