iOS Performance Tuning series: Discovering performance bottlenecks with time profiler

Source: Internet
Author: User

Hard Canton: "iOS Performance Tuning Series" Fifth, is expected to be more than 20, continuously updated, welcome attention.

The previous four articles focused on memory, analyzed memory leaks, zombie objects, memory allocations, and this article describes the use of Time Profiler tools to start a true "performance" tuning journey.

Time Profiler, as well as the leaks and allocations tools previously introduced, is dubbed the "life-saving" of instruments, which is the first three tools to use when the application encounters a problem.

Time Profiler helps us analyze the execution times of the code, find out why the program is slowing down, and tell us "Where did the time go?" ”。

Before using time Profiler, take a look at the original profiling method.

The original performance analysis method

This analysis method is estimated to be the first time many developers think of, write a unit test, at the beginning and end of the place to record time.

Example code:

NSDate *stdate = [nsdate date];for (int i = 0; i < 999; i++) {//do something   }nsdate *enddate = [NSDate Date]; NSLog (@ "time:%f", [EndDate timeintervalsincedate:stdate]);

By calculating the time difference, you can work out the intermediate consumption, and in this way, you can analyze some of the code that might appear to be a performance bottleneck.

The disadvantages of this approach have the following highlights:

1, testing efficiency is too low, many performance bottlenecks are difficult to estimate, need to be removed from the upper layer to the lower level;

2, unable to test the efficiency of interface rendering, to find the interface performance bottleneck;

3, NSLog analysis is not accurate enough, may be in the simulator due to the development of equipment performance speed, can not clearly distinguish the performance bottleneck.

This is also the advantage of using the time Profiler tool to identify performance bottlenecks with extreme efficiency.

Performance analysis methods using Time Profiler

Time Profiler Analysis principle: It tracks the stack information for each thread at regular intervals, calculates how long a method executes and obtains an approximation by statistically comparing the stack state between intervals. In fact, it is basically the same as our original analysis method, but it will be the time spent in each method statistics.

As with other tools that use instruments, click the Xcode Product menu profile to start instruments:

Select the Time Profiler tool to start the test, and the simulator and time Profiler recording will be started automatically.

Start with some apps and let time Profiler collect enough data, especially where you think there are performance bottlenecks.

4, 5, 6 labeled Panel is to be concerned about:

4 is the expansion panel, used to track the display stack, 5 is the detailed panel, you can see from here where the CPU is running, and 6 is the options panel, you can set the time Profiler to run parameters.

By manipulating the app, you can see what the most time-consuming actions are in the detail panel, and you can expand the view on a line-by-row basis:

The icon for the black avatar is the cue that time Profiler gives us, where there may be a performance bottleneck where you can gradually expand down to find the root cause.

For example, through analysis, we find that [Cmtool Getnewstimefromlong] This time format function may need to be optimized.

Of course, here is just one example, performance tuning should be done first from the whole to the details of the order to receive the most obvious effect.

Time Profiler parameter settings

Here are a few options that have the following meanings:

Separate by thread: each thread should be considered separately. Only in this way can you pull out the heavy threads that consume a lot of CPU

Invert call Tree: From the top down the trace stack, which means that the method you see in the table will have been sampled from frame No. 0, which is usually what you want, so you can see the most time-out method in the CPU. That is, funca{funb{func}} Tick the stack with c->b-a to show the deepest C at the outermost level of the call

Hide Missing Symbols: If dSYM can't find your app or system frame, you can't see the method name in the table, only 16 binary values are visible, and if this item can hide the symbols, it's easy to simplify the data

Hide System Libraries: Check this to show your app's code, which is very useful. Because usually you only care about the time the CPU spends on its own code, not on the system.

Show Obj-c only: Show OC code only, if your program is a program like OpenGL, do not tick sideways because he might be C + +

Flatten recursion: Recursive function, one entry per stack trace

Top Functions: The sum of the time that a function spends directly in the function, and the total time it takes for the function to call the function. So, if function A calls B, then A's time to report a time spent in a plus B takes time, which is really useful because it allows you to pick the maximum number of times each time you go down to the call stack, zeroing in on your most-time-consuming method.

The above parameters are set rationally in practice, and there is not much skill, that is, by hiding and displaying data, we focus more on the data we want to find.

The world application, only fast not broken! Especially in mobile applications, you should pay more attention to the user experience and response speed.

Record, for the better of myself!

iOS Performance Tuning series: Discovering performance bottlenecks with time profiler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.