Skip to content

Instantly share code, notes, and snippets.

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