Skip to content

Instantly share code, notes, and snippets.

@joncardasis
Last active January 26, 2018 00:26
Show Gist options
  • Select an option

  • Save joncardasis/1ccb66f024538a2568495381bed6d27b to your computer and use it in GitHub Desktop.

Select an option

Save joncardasis/1ccb66f024538a2568495381bed6d27b to your computer and use it in GitHub Desktop.
My Swift 3 Measure Execution time function using GCD
func measureBlock(_ description: String, numberOfExecutes: Int = 1, block: () -> Void) {
let start = DispatchTime.now()
for _ in 0..<numberOfExecutes {
block()
}
let end = DispatchTime.now()
let nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds
let secondsTime = (Double(nanoTime)/1000000000.0)
let avgNanoTime = nanoTime / UInt64(numberOfExecutes)
let avgSecTime = secondsTime / Double(numberOfExecutes)
print("\(description):")
print(" Total Time was \(nanoTime)ns (\(secondsTime))s")
print(" Took average of \(avgNanoTime)ns (\(avgSecTime)s) per block.")
print(" Ran block \(numberOfExecutes) time(s)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment