Benchmark Swift Code

Make it better!

Steven Curtis
3 min readJan 11, 2023
Photo by Melyna Valle on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

This article has been developed using Xcode 14.2, and Swift 5.7.2

Xcode has Instruments that can be used to improve your App’s performance and get it working just as it should.

That is great, however sometimes we need to go further. Sometimes we need to *benchmark* our code.

Terminology

Benchmark: The act of running a program in order to assess the relative performance

The Clue In The Code

Once you’ve created a new project you automatically get some tests written for you.

You’ll get the following code snippet in your project

func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

Which is super nice! You write your code in here that you wish to test! Wow!

However there is something *wrong* about this. At least potentially.

You should always measure speed setting the optimization is set to [-Os].

--

--

No responses yet