Last active
October 28, 2016 18:27
-
-
Save pyrtsa/e1b89307c1e7b30ef841 to your computer and use it in GitHub Desktop.
Profile a block of code in Swift 2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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