How to Measure and Compare Execution Time in Swift and iOS

How to Measure and Compare Execution Time in Swift and iOS

When you're developing an app, writing it for low-end devices with high network latency makes sure your app does not provide a degraded performance for some users.

In order to know how performant our app is, we can add support to provide a timing summary in terms of execution time and verify if it matches the expectation.

In today's blog post, we will see how to measure and compare execution time for a given block of code in Swift and iOS.

Measuring Execution Time

To measure execution time for a given block of code, we will write a function named measureTime which will take a closure as an input parameter. It will start the clock just before closure execution, execute the closure and stop the clock and print how much time it took for the execution in seconds.


func measureTime(for closure:() -> Void) {
    let start = CFAbsoluteTimeGetCurrent()
    closure()
    let difference = CFAbsoluteTimeGetCurrent() - start
    print("Took \(difference) seconds")
}

Now with this function ready, let's write a sample for-loop and measure its execution time using measureTime utility.


measureTime{
    for i in 0..<10000 {
        print("1000")
    }
}

Output

Prints Took 0.041365981101989746 seconds

Comparing Execution Time

Sometimes calculating execution time is not enough. We want to make sure app lives up to the high-performance standard and we want to catch when there is a performance regression. To achieve this, we can pass the expected execution time parameter to measureTime function and compare it with the actual execution time. We can also assert that the actual execution time should be less than or equal to the expected execution time. If violated, it will stop the program execution in the debug mode.


func measureTime(for expectedExecutionTime: Double? = nil, closure:() -> Void) {
    let start = CFAbsoluteTimeGetCurrent()
    closure()
    let difference = CFAbsoluteTimeGetCurrent() - start

    if let expectedExecutionTime {
        assert(difference <= expectedExecutionTime)
    }

    print("Took \(difference) seconds")
}

And we can execute it like this,


measureTime(for: 1.0) {
    for i in 0..<10000 {
        print(i)
    }
}
💡
Now whenever the execution time exceeds 1 second, the assert will trigger and we can directly catch the performance regression during debug mode

Summary

That's all folks for today's very short blog post. Now you know how to measure and compare execution time for any block of code in your iOS app. If you have any questions or comments, feel free to reach out to me on LinkedIn.  If you like my blog content and wish to keep me going, please consider donating on Buy Me a Coffee or Patreon. Help, in any form or amount, is highly appreciated and it's a big motivation to keep me writing more articles like this.