Is there a way/software to give precise time needed to execute a block of code written in Swift, other than the following?
let date_start = NSDate()
// Code
I like Brad Larson's answer for a simple test that you can even run in a Playground. For my own needs I've tweaked it a bit:
averageTimeTo() benchmarking function.averageTime (which you might expect from a function called 'averageTimeTo'), so there's no need for two separate functions that have almost identical functionality.For example:
func myFunction(args: Int...) {
// Do something
}
func testMyFunction() -> String {
// Wrap the call to myFunction here, and optionally test with different arguments
myFunction(args: 1, 2, 3)
return #function
}
// Measure average time to complete test
func averageTimeTo(_ testFunction: () -> String, repeated reps: UInt = 10) -> Double {
let functionName = testFunction()
var totalTime = 0.0
for _ in 0..