Created
June 20, 2023 19:50
-
-
Save ivanopcode/efcf27d0aaf988994e200a5ca6d09996 to your computer and use it in GitHub Desktop.
Convince wrapper for measuring function execution time, relies on Darwin's Foundation
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
// MIT | |
// Alexey Grigorev, Ivan Oparin | |
import Foundation | |
@discardableResult | |
public func measure(_ action: () -> ()) -> Double { | |
let start = Date() | |
action() | |
let end = Date() | |
return end.timeIntervalSince(start) | |
} | |
@discardableResult | |
public func measure(_ action: () async -> ()) async -> Double { | |
let start = Date() | |
await action() | |
let end = Date() | |
return end.timeIntervalSince(start) | |
} | |
public func measure(_ action: () -> (), log: (TimeInterval)->()) { | |
let start = Date() | |
action() | |
let end = Date() | |
log(end.timeIntervalSince(start)) | |
} | |
public func measure(_ action: () async -> (), log: (TimeInterval)->()) async { | |
let start = Date() | |
await action() | |
let end = Date() | |
log(end.timeIntervalSince(start)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment