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, block: () -> Void) {
var measures: [TimeInterval] = []
for i in (0..<1_010) {
let start = Date()
block()
if i >= 10 { // 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