Last active
July 23, 2018 08:11
-
-
Save V8tr/530c8c79053f90d17cf5214e16b17413 to your computer and use it in GitHub Desktop.
Average benchmark for arbitrary block of code in Swift. Used to compensate deviations of individual benchmark iterations. See blog post for more details: http://www.vadimbulavin.com/benchmarking-locking-apis/
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
func averageBenchmark(iterations: Int, numberOfAttempts: Int, block: (Int) -> Void) -> TimeInterval { | |
var accumulatedResult: TimeInterval = 0 | |
for _ in 0..<numberOfAttempts { | |
let result = benchmark { | |
for i in 0..<iterations { | |
block(i) | |
} | |
} | |
accumulatedResult += result | |
} | |
return accumulatedResult / TimeInterval(numberOfAttempts) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment