Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Last active October 28, 2016 18:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pyrtsa/e1b89307c1e7b30ef841 to your computer and use it in GitHub Desktop.
Save pyrtsa/e1b89307c1e7b30ef841 to your computer and use it in GitHub Desktop.
Profile a block of code in Swift 2.0
import Foundation
public func profiling<R>(label: String, @noescape _ block: () -> R) -> R {
NSLog("*** %@...", label)
let start = NSDate()
defer {
let end = NSDate()
NSLog("*** %@ took %5.3g seconds", label, end.timeIntervalSinceDate(start))
}
return block()
}
let result: Int? = profiling("heavy computation") {
NSLog("Really computing hard now")
return Int("123")
}
// 2015-06-09 22:32:48.811 repl_swift[39408:2024531] *** heavy computation...
// 2015-06-09 22:32:48.811 repl_swift[39408:2024531] Really computing hard now
// 2015-06-09 22:32:48.812 repl_swift[39408:2024531] *** heavy computation took 0.000195 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment