Skip to content

Instantly share code, notes, and snippets.

@hishma
Created September 3, 2019 17:17
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 hishma/db12b03b9a31945150a3ea946baea3f6 to your computer and use it in GitHub Desktop.
Save hishma/db12b03b9a31945150a3ea946baea3f6 to your computer and use it in GitHub Desktop.
Prints the time taken to execute a closure.
/// Prints the time taken to execute a closure.
///
/// Note: Only for debugging purposes.
public func measure<T>(_ label: String = "", _ f: () throws -> (T)) rethrows -> T {
let startTime = Date()
let result = try f()
let endTime = Date().timeIntervalSince(startTime)
print("\(label): Time taken", endTime)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment