Last active
August 20, 2020 16:09
-
-
Save achernoprudov/e3152e30063345353b0e70ef51fbcc0a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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