Skip to content

Instantly share code, notes, and snippets.

@nbasham
Created April 22, 2019 23:45
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 nbasham/d5bb9671e8e74abb1f8387575d8c8e46 to your computer and use it in GitHub Desktop.
Save nbasham/d5bb9671e8e74abb1f8387575d8c8e46 to your computer and use it in GitHub Desktop.
A simple way to time code using Swift.
import Foundation
class CodeTimer {
var startTime: CFAbsoluteTime = 0
init() { start() }
func start() { startTime = CFAbsoluteTimeGetCurrent() }
func log(_ message: String) {
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
let s = String(format: "%.3f", timeElapsed)
print("\(message) \(s) second(s).")
}
}
@nbasham
Copy link
Author

nbasham commented Apr 22, 2019

Example usage:

    let timer = CodeTimer()
    timer.log("Time elapsed:") // Time elapsed: 0.380 second(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment