5 Simple Steps to Find Slow Code Using Xcode Time Profiler

It is easier than you think

Lee Kah Seng

--

This article is originally published at swiftsenpai.com

Xcode Time Profiler is a powerful performance analysis tool provided by Apple. It is specifically designed to help developers optimize the performance of their iOS, macOS, watchOS, and tvOS applications.

If you open the Time Profiler for the first time, you might feel overwhelmed by its complex user interface and the abundance of data presented to you. To assist you in getting started, here are five simple steps you can follow to identify the bottleneck in your code:

  1. Profile Your App
  2. Filter the Track Viewer
  3. Simplified the Call Tree
  4. Analyze the Heaviest Stack Trace
  5. Reveal Source Code in Xcode

Now, let’s go through these steps one by one, shall we?

The Sample App

To demonstrate the capabilities of the Time Profiler, I have created a sample app exclusively for this article. This app features a single button that, when tapped, loads two text files repeatedly for a total of 1000 times.

@IBAction func loadFiles(_ sender: Any) {

for _ in 0..<1000 {

// Load text file with size: 74KB
let url = Bundle.main.url(forResource: "small_text_file",
withExtension: "txt")!
let _ = try! String(contentsOf: url)

// Load text file with size: 885KB
let url2 = Bundle.main.url(forResource: "large_text_file",
withExtension: "txt")!
let _ = try! String(contentsOf: url2)
}
}

As you can see, the text files being loaded have different file sizes. We anticipate that large_text_file.txt will take longer to load compared to the small_text_file.txt.

In just a moment, we will use the Time Profiler to verify this assumption and examine the execution times of these operations.

Step 1: Profile Your App

To begin profiling your app in Xcode, navigate to the “Product” → “Profile,” or simply press ⌘ + I. After the build process…

--

--

Lee Kah Seng

Support me by becoming a Medium member: https://leekahseng.medium.com/membership ⦿ 🇲🇾 Creator of https://swiftsenpai.com ⦿ iOS developer since 2011