Last active
May 30, 2024 19:39
-
-
Save nakajima/de574af6041d46c4d1021e55e92623d5 to your computer and use it in GitHub Desktop.
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 OSLog | |
// Measure how long stuff takes. | |
public enum Benchy { | |
public static func measure<T>(_ label: String, logger: Logger = Logger(subsystem: "Benchy", category: "measure"), block: () throws -> T) rethrows -> T? { | |
let clock = ContinuousClock() | |
var value: T? | |
let result = try clock.measure { value = try block() } | |
logger.trace("\(label) took \(result)") | |
return value | |
} | |
public static func measure<T>(_ label: String, logger: Logger = Logger(subsystem: "Benchy", category: "measure-async"), block: @Sendable () async throws -> T) async rethrows -> T? { | |
let clock = ContinuousClock() | |
var value: T? | |
let result = try await clock.measure { value = try await block() } | |
logger.trace("\(label) took \(result)") | |
return value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment