Skip to content

Instantly share code, notes, and snippets.

@V8tr
Last active July 23, 2018 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save V8tr/530c8c79053f90d17cf5214e16b17413 to your computer and use it in GitHub Desktop.
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/
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