Skip to content

Instantly share code, notes, and snippets.

@jimmyhoran
Created June 4, 2019 11:14
Show Gist options
  • Save jimmyhoran/f4a08647784dbee5e1ec5da945e3a17c to your computer and use it in GitHub Desktop.
Save jimmyhoran/f4a08647784dbee5e1ec5da945e3a17c to your computer and use it in GitHub Desktop.
Helper functions to benchmark Swift code execution time
import UIKit
func printTimeElapsedExecuting(title: String, operation: () -> Void) {
let startTime = CFAbsoluteTimeGetCurrent()
operation()
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
print("Time elapsed for \(title): \(timeElapsed)s.")
}
func timeElapsedInSecondsExecuting(operation: () -> Void) -> Double {
let startTime = CFAbsoluteTimeGetCurrent()
operation()
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
return Double(timeElapsed)
}
printTimeElapsedExecuting(title: "\"(0..<999).map()\"") {
_ = (0..<999).map { pow(sin(CGFloat($0)), 10.0) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment