Skip to content

Instantly share code, notes, and snippets.

@achernoprudov
Last active August 20, 2020 16:09
Show Gist options
  • Save achernoprudov/e3152e30063345353b0e70ef51fbcc0a to your computer and use it in GitHub Desktop.
Save achernoprudov/e3152e30063345353b0e70ef51fbcc0a to your computer and use it in GitHub Desktop.
/// Watcher for time measure.
///
/// ! Non thread safe
public class TimeWatcher {
// MARK: - Static
public enum Interval: UInt64 {
case nanos = 1
case mills = 1_000_000
case seconds = 1_000_000_000
}
// MARK: - Private
private static var currentMills: DispatchTime {
return DispatchTime.now()
}
// MARK: - Instance variables
var startTime: DispatchTime = TimeWatcher.currentMills
// MARK: - Public
public init() {
self.startTime = TimeWatcher.currentMills
}
/// Resets initial time
public func start() {
self.startTime = TimeWatcher.currentMills
}
/// Returns diff between initial time and now
public func measure(_ interval: Interval = .mills) -> UInt64 {
let diff = TimeWatcher.currentMills.rawValue - startTime.rawValue
return UInt64(diff / interval.rawValue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment