Created
June 4, 2019 11:14
-
-
Save jimmyhoran/f4a08647784dbee5e1ec5da945e3a17c to your computer and use it in GitHub Desktop.
Helper functions to benchmark Swift code execution time
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
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