-
-
Save saroar/8b3796be80a660ed5ce790d856da84b4 to your computer and use it in GitHub Desktop.
Benchmark Swift code execution
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
/* | |
The former will log out the time required for a given section of code, with the latter returning that as a float. | |
Read more : http://stackoverflow.com/questions/25006235/how-to-benchmark-swift-code-execution | |
*/ | |
func printTimeElapsedWhenRunningCode(title:String, operation:()->()) { | |
let startTime = CFAbsoluteTimeGetCurrent() | |
operation() | |
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime | |
print("Time elapsed for \(title): \(timeElapsed)s") | |
} | |
func timeElapsedInSecondsWhenRunningCode(operation:()->()) -> Double { | |
let startTime = CFAbsoluteTimeGetCurrent() | |
operation() | |
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime | |
return Double(timeElapsed) | |
} | |
// Example | |
printTimeElapsedWhenRunningCode("linerSearh()") { | |
linerSearh(5631) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment