Created
April 3, 2018 01:35
-
-
Save dedeexe/16e01851567730c3cc5e74fc5911e879 to your computer and use it in GitHub Desktop.
Time Counter
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
struct TimeCounter { | |
private var lastTime : Date? | |
private(set) var counters : Int = 0 | |
private(set) var passedTime : TimeInterval = 0.0 | |
mutating func add() { | |
guard let previousTime = lastTime else { | |
lastTime = Date() | |
counters += 1 | |
return | |
} | |
passedTime += Date().timeIntervalSince(previousTime) | |
lastTime = Date() | |
counters += 1 | |
} | |
var average : TimeInterval { | |
guard counters > 1 else { return 0 } | |
return passedTime / Double(counters) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment