Skip to content

Instantly share code, notes, and snippets.

@dedeexe
Created April 3, 2018 01:35
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 dedeexe/16e01851567730c3cc5e74fc5911e879 to your computer and use it in GitHub Desktop.
Save dedeexe/16e01851567730c3cc5e74fc5911e879 to your computer and use it in GitHub Desktop.
Time Counter
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