Skip to content

Instantly share code, notes, and snippets.

@saroar
Forked from minikin/benchmarkSwift.swift
Created September 29, 2019 09:10
Show Gist options
  • Save saroar/8b3796be80a660ed5ce790d856da84b4 to your computer and use it in GitHub Desktop.
Save saroar/8b3796be80a660ed5ce790d856da84b4 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