Skip to content

Instantly share code, notes, and snippets.

@ivanopcode
Created June 20, 2023 19:50
Show Gist options
  • Save ivanopcode/efcf27d0aaf988994e200a5ca6d09996 to your computer and use it in GitHub Desktop.
Save ivanopcode/efcf27d0aaf988994e200a5ca6d09996 to your computer and use it in GitHub Desktop.
Convince wrapper for measuring function execution time, relies on Darwin's Foundation
// 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