Profiling is a means of measuring the space (memory) or the time complexity of a program. By completing a profiling session, iOS developers are able to see which parts of the code are used most often or need improvements.
Instead of wasting time on making random assumptions, iOS developers should spend the effort optimising the loop in which their program allocates more time.
Let’s take the following scenario: you notice that your app doesn’t run as fast as you expect it to and you start wondering where the problem comes from. Based on some of your hypotheses, you decide to spend a week or two making adjustments based on an interesting algorithm. After you’re done, you realize that the part of code you’ve been working on occupies only 0,5% of the total execution time. Therefore, your effort was kind of aimless because you didn’t look into the biggest problem.
Many developers skip the analysis part and risk to waste important resources by making the same mistake. In order to avoid this kind of situation, Tudor, one of our iOS developers, tells us to include Time Profiler in our iOS developer kit. This is an Xcode instrument that, at measured intervals, halts the execution of the program. A stack trace is performed on each running thread:
When it comes to code optimization, Time Profiler is a very powerful tool. It saves you time by looking into the parts of your code that need improvement. The main idea of using it comes from the following strategy: always try to solve the things with the biggest impact in order to perform.
In a nutshell, the tool has 3 main features:
- it measures the space (memory) or time complexity of a program;
- it measures the usage of particular instructions, or frequency and duration of function calls;
- the most common use of profiling information is to aid program optimization.
How does it work?
After you selected the Time Profiler icon from the Xcode, you are ready to run your application on your device. By clicking the “All Processes” button, you’ll be able to select that certain application that you want to test. Whenever the record button is pressed, the app is launched on the phone and the profiling process begins.
3 things that you must pay attention to:
1. Profiler Preferences
For a clear view, Profiler offers you the possibility to change some of the “Call Tree” settings. If you, for example, select Objective C only, Instruments will generate a list of operations which take place in your app. Each operation comes with a percentage of time spent completing each process. This Profiler’s feature helps you follow the high percentage marks and identify the problem. If you double click on each operation, the tool takes you to that specific line of code that needs improvement. After figuring out what the issues are, the solution will come right away.
2. Timeline Flags
During the recording process, you can add user-flags in order to remember where to come back and enhance the quality of some features. Knowing the exact places that you need to focus on saves you lots of research hours.
3. Focus Window
If you want to make your app run faster, you should definitely go through certain time periods or functions in order to define the issue. For a closer look at a specific operation, you have to click on the arrow next to it. The tool moves its focus from analyzing the app to analyzing that single operation. This option is really useful when improving a single view controller’s functionality.
There’s another way that leads to the same results. You can select a certain window of time that you want to investigate. By holding option over the Time Profiler line and clicking on your area of interest, the tool only shows the operations running during that time frame.
There are two important things that developers can do to increase their code quality:
1. Detect and fix memory management problems in the code by using Instruments
2. Determine hot-spots in the code by using the Time Profiler Instrument in order to work efficiently