Skip to content

Instantly share code, notes, and snippets.

@ncreated
Last active November 4, 2022 21:29
Show Gist options
  • Save ncreated/db931ed0202721b50fab56c3ccdcd4e8 to your computer and use it in GitHub Desktop.
Save ncreated/db931ed0202721b50fab56c3ccdcd4e8 to your computer and use it in GitHub Desktop.
Basic XCTestCase Benchmark
extension XCTestCase {
func benchmark(_ label: String, iterations: Int = 100, block: () throws -> Void) rethrows {
var measures: [TimeInterval] = []
let warmUp = iterations / 10
for i in (0..<iterations) {
let start = Date()
try block()
if i >= warmUp { // only measure after it is warmed-up
let stop = Date()
measures.append(stop.timeIntervalSince(start))
}
}
let sum = measures.reduce(0, +)
let avg = sum / Double(measures.count)
print("⭐️ AVG in \(label): \(avg * 1_000) ms")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment