Last active
September 29, 2019 09:10
-
-
Save minikin/398c7ef65bb14a6137bb to your computer and use it in GitHub Desktop.
Benchmark Swift code execution
This file contains 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