Last active
January 26, 2018 00:26
-
-
Save joncardasis/1ccb66f024538a2568495381bed6d27b to your computer and use it in GitHub Desktop.
My Swift 3 Measure Execution time function using GCD
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
| 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