Find the Slow Spots in Your Code with the CPU Profiler Instrument

If you find your Swift app is running slowly, Instruments includes a CPU Profiler instrument to help you find the code causing your app to run slowly. This article shows you how to use the CPU Profiler instrument to find the slow code in your app.

Launching Instruments

In Xcode choose Product > Profile or press Cmd-I to build your Xcode project and launch Instruments.

When Instruments launches a window opens for you to choose a template to profile your app. Select the CPU Profiler template and click the Choose button. If you are running an older version of Xcode that does not have the CPU Profiler template, select the Time Profiler template.

Profiling Your App

Click the Record button at the top of the trace document window or press Cmd-R to start profiling your app. Run your app.

Click the Pause button or press Cmd-Shift-R to pause recording. Click the Stop button or press Cmd-Dot to stop recording.

Reading the Profiling Data

When you stop recording Instruments shows the call tree view.

CPU Profiler call tree view

The call tree view has a listing for each function that executed while profiling.

Finding Your Code in the Call Tree

When you first look at the call tree view, you will see it shows lots of functions that you didn’t write. How do you find your functions in the call tree view?

To find your code in the call tree view, click the Call Tree button at the bottom of the window and click the Hide System Libraries and Invert Call Tree checkboxes.

CPU Profiler invert call tree

Hiding the system library code makes it easier to find your app’s functions in the call tree view. Inverting the call tree brings the executed functions to the top of the call tree view.

Finding the Slow Code

How do you know which functions are the slow functions?

Each function in the call tree view has a Weight value and a Self Weight value. The Weight value tells you how long the function was on the call stack. The Self Weight value tells you how long the function was at the top of the call stack.

The Self Weight value is more important. Suppose you have a function A that calls functions X, Y, and Z. The Weight value for A tells you the amount of time your app was in functions A, X, Y, and Z. The Self Weight value for A tells you the amount of time your app was in A. When you’re looking for the slow spots in your code, look at functions with a high Self Weight value.

Clicking the Self Weight column heading sorts the call tree view listings by their Self Weight value. Sorting by Self Weight brings the slow functions to the top of the call tree view.

Viewing the Slow Code

Double-clicking a function in the call tree view opens the source view, where you can see the lines of code that take the most time in the function. Make sure you double-click a function you wrote. If you double-click a function you didn’t write, the source view shows unreadable assembly language code.

Do you want to learn more about Instruments?

I wrote a book, Profiling Swift Apps, that shows you how to use Instruments to find and fix problems in your code. The book will help you make apps that run faster and use less memory. You can learn more about the book and download a sample at the following link:

Profiling Swift Apps page

Get the Swift Dev Journal Newsletter

Subscribe and get exclusive articles, a free guide on moving from tutorials to making your first app, notices of sales on books, and anything I decide to add in the future.

    We won't send you spam. Unsubscribe at any time.