Swift version: 5.6
Performance tests let you check how fast your code runs, but more importantly it lets you check how fast your code runs over time – you can spot performance changes as your code evolves.
Writing a performance test takes just two steps:
measure()
method inside that test, running any work you want.To try it out, look in the “Tests” group in your Xcode project, then open your tests file. In my test I’m going to try generating images using a fictional ImageGenerator
struct that has a generateImages()
method. I’m specifically going to be testing the generateImages()
method, which means I’ll create a test instance of ImageGenerator
outside the measure()
method, like this:
func testPerformanceExample() {
let generator = ImageGenerator()
measure {
generator.generateImages()
}
}
When that test runs, Xcode will run the contents of measure()
10 times to get a spread of results.
SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.