Last active
November 4, 2022 21:29
-
-
Save ncreated/db931ed0202721b50fab56c3ccdcd4e8 to your computer and use it in GitHub Desktop.
Basic XCTestCase Benchmark
This file contains 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
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