Guides and Sample Code

Developer

Instruments User Guide

On This Page

Measure CPU Use

Effective use of the CPU allows your app to run faster and more effectively. Although you are writing an app for a particular platform, keep in mind that even the same general platform has different CPU capabilities. Instruments helps you conduct testing under a variety of conditions, allowing you to identify how well your app uses multiple cores, threads, and other resources.

Track Core and Thread Use

The Time Profiler profiling template uses the Time Profiler instrument to perform low-overhead, time-based sampling of processes running on the system’s CPUs. The more effectively multiple cores and threads are used over time, the better an app’s performance. The CPU and thread strategies in the Time Profiler instrument show how well an app utilizes cores and threads.

To view core or thread usage
  1. Launch Instruments.

  2. In the profiling template selection dialog that appears, click Time Profiler.

  3. Choose your device and app from the target device and process lists.

  4. Click Choose to create a trace document.

  5. Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.

  6. Use your app or the system normally.

  7. Click the Stop button (image: ../Art/inline_stop_button_2x.png), or press Command-R again, when complete.

  8. Click the CPU or threads strategy button in the trace document toolbar.

  9. Examine the collected data.

Use the CPU strategy usage view to compare core usage over given time periods. Effective core concurrency improves an app’s performance. Look for unbalanced core usage. If a single core has areas of heavy usage while other cores remain quiet, that can indicate areas that need greater optimization. When zoomed out, activity may appear to be occurring on multiple threads, but a closer inspection may reveal otherwise. Therefore, be sure to zoom in on the track pane when examining core usage.

Use the threads strategy view to examine your app’s use of threads when performing work. Large amounts of work on the main thread can make your app’s user interface unresponsive or slow. Whenever possible, work should be moved off the main thread.

Look for Performance Bottlenecks

Performance monitor counters (PMCs) are hardware registers that measure events occurring in the processor. They help find bottlenecks in your app by identifying an excessive number of events of a particular type. For example, a high number of conditional branch instructions may indicate a section of logic that, if rearranged, might lower the number of branches required. PMC events bring these issues to light, but it is up to you to match them to your code and decide how they will help you improve your app’s performance. The Counters profiling template uses the Counters instrument to track PMC events.

To track PMC events
  1. Launch Instruments.

  2. In the profiling template selection dialog that appears, click Counters.

  3. Choose your device and app from the target device and process lists.

  4. Click Choose to create a trace document.

  5. Click the Counters instrument in the timeline pane.

  6. Press Command-2 to show record settings in the inspector pane.

  7. In the record settings area of the inspector pane, click the Add button (+) in the Events and Formulas table.

  8. Select the event you’d like to count.

  9. Repeat steps 7 and 8 to add more events, if desired.

  10. Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.

  11. Use your app or the system normally.

  12. Click the Stop button (image: ../Art/inline_stop_button_2x.png), or press Command-R again, when complete.

  13. Examine the collected data.

Other Ways to Measure CPU Use

Instruments provides a number of other profiling templates that can be used to examine your app’s use of the CPU.

  • Use the Activity Monitor profiling template to gauge overall CPU use in comparison to other apps.

  • Use the Dispatch profiling template to see when your dispatch queues are executed. You can see how long the dispatched thread lasts and how many blocks are used.

  • Use the System Trace profiling template to see when threads are scheduled, time active, core used, and more.