Instruments is a great tool provided by Apple to let developers hone their applications for the best performance. While incredibly useful, many developers avoid the application for fear of not knowing all the ins and outs of its functionality. Today, I have a few quick tips that you can use in some very basic Instruments use cases. Let's dive in!
The Time Profiler
When navigating around your application, you may notice some delays that you would like to reduce to make the best user experience. If you wanna investigate where the OS is spending time as your are using an application, then the Time Profiler is for you. The time profiler is the place where most developers get the quickest value from Instruments, so we are going to focus our tips on this tool, although many of them apply to the other instrument tools as well. To begin, you will open Instruments and be greeted with this menu:
As you might guess, we are going to select the Time Profiler. For this to give you valuable feedback you should be running your application on your device. With your application running, you should click on the field that says "All Processes" in the top right hand corner. Then select the specific application you want to Time Profile.
With this done, we can begin our Time Profile tests by hitting the record button. This will launch the application on the phone and you will begin time profiling. Tap around on you application and use it, you will see a growing stack of classes, symbols, and memory addresses. Kinda hard to make heads or tails of, huh? Let's make this data a bit more clear.
Tip 1: Configure Profiler Preferences
You are going to want to change some of the "Call Tree" settings on the left side of Instruments. This will make the Instruments read out much clearer to read.
You should first turn off inverting the call tree and next ask it to show objective C only. For most projects this will be the most productive view of you data. With this done, Instruments will present to you a top down look at the different operations happening within your application, along with a percentage of time spent handling each process. If you are looking to speed up your application, you can follow the high percentage marks down to your specific classes. For my test, I can see that most of my time is being spent in my SignatureViewCell touchesMoved:withEvent: method. If I wanted to find places to optimize, that would be the best place to look. Double clicking on a line will even bring you to the specific code it is referencing:
So, now we know how to look at the reporting from Time Profiler and learn from it. But how do we get more insight into when certain operations occurring during our test? Well, Instruments allows you to "flag" as your are tapping along on your device to mark when you did certain activities.
Tip 2: Adding Timeline Flags
As you are using your application and Time Profiler is recording, you can hold option and click on the ticks just above the timeline. This will add user-flags where you can annotate the actions you were doing at that point during recording. This will come in handy when you are trying to focus on a specific transition, animation, or other operation at runtime.
So this is all great, but what about focusing on a small section of this entire execution. If I wanted to focus on a single animation it would be difficult to get instruments to focus in on that. Luckily this functionality is provided as well.
Tip 3: Setting a Focus Window
Instruments allows you to focus in on certain time periods or operations for higher resolution timing reports. If you want to "zoom" into a specific operation you can click on the arrow next to it, that will change the percentages associated with operations to be based only on that parent operation and its children. This is really useful when focusing on a single view controller's functionality. The other way to focus in is by selecting a certain window of time that you would like to investigate. This is accomplished by holding option over the time profiler time line and clicking and dragging the area of interest. This will filter the call tree to only show operations during that time window. This in conjunction with user flags can help you get to the heart of an issue very quickly.