Skip to content

Instantly share code, notes, and snippets.

@barbietunnie
Forked from pyrtsa/Profiling.swift
Last active August 29, 2015 14:24
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 barbietunnie/61423bbc149a9ff00d28 to your computer and use it in GitHub Desktop.
Save barbietunnie/61423bbc149a9ff00d28 to your computer and use it in GitHub Desktop.
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