Skip to content

Instantly share code, notes, and snippets.

@sokol8
Created January 28, 2023 23:20
Show Gist options
  • Save sokol8/f6201acb94ef3f0c20931156d871ea1b to your computer and use it in GitHub Desktop.
Save sokol8/f6201acb94ef3f0c20931156d871ea1b to your computer and use it in GitHub Desktop.
Calculate execution time for a block of Swift code
// 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