Skip to content

Instantly share code, notes, and snippets.

@aheze
Created February 12, 2024 16:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheze/b7b4eb9e6f1c7258b6284c86f8041993 to your computer and use it in GitHub Desktop.
Save aheze/b7b4eb9e6f1c7258b6284c86f8041993 to your computer and use it in GitHub Desktop.
class TimeElapsed: CustomStringConvertible {
private let startTime: CFAbsoluteTime
private var endTime: CFAbsoluteTime?
init() {
startTime = CFAbsoluteTimeGetCurrent()
}
var description: String {
time
}
var time: String {
let format = String(format: "%.5f", duration)
let string = "[\(format)s]"
return string
}
var duration: Double {
let endTime = CFAbsoluteTimeGetCurrent()
return endTime - startTime
}
}
let timer = TimeElapsed()
// do some lengthy task here
print("Time elapsed: \(timer)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment