Created
January 28, 2023 23:20
-
-
Save sokol8/f6201acb94ef3f0c20931156d871ea1b to your computer and use it in GitHub Desktop.
Calculate execution time for a block of Swift code
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
// based on gist https://gist.github.com/kristopherjohnson/4201fbe86473f6edb207 | |
// and discussion | |
// https://forums.swift.org/t/recommended-way-to-measure-time-in-swift/33326/4 | |
// https://github.com/apple/swift-corelibs-xctest/pull/109 | |
// Ensures Monotonic time | |
public func executionTimeInterval(block: () -> ()) -> TimeInterval { | |
let start = Foundation.ProcessInfo.processInfo.systemUptime | |
block(); | |
let end = Foundation.ProcessInfo.processInfo.systemUptime | |
return end - start | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment