Xcode Instrument: Time Profiler
Astounding effects of Xcode instruments has been discussed earlier also. Nevertheless to say, a huge share of iOS applications benefited from it.
TL;DR
Time profiler is an important instrument which helps in achieving better performance in iOS applications. It provides details about time consumed by each method in your source code, giving able space to alter the logic applied. Developers can use this cluster of information to redefine the existing algorithms.
Kick Start: Time Profiler in Developer world
Open your source code in Xcode and go to Product and select Profile to start Profiling with Instruments, You can use shortcut key Command + I to do the same. Once building is complete, you will see Instruments Box to select from, as shown below:
This opens up our host of the night, Time profiler. Now click on Record button on top right corner to start profiling.
Once you run through your application during the recording session, profiler will start capturing the method signatures. On completion, click on Stop button and you can observe the details in details panel.
Terms that you see in detail panel
Weight: Describes the overall time taken executed by parent method if it contains nested methods. Let us say method A calls methods B and C. Then weight gives the time taken to execute method A which includes the time for B and C too.
Self weight: Describes the weight of a single method. By drilling down to method A in our previous case, you can observe the weight taken by methods B and C, which will be reflected in Self weight.
Symbol name: Represents thread name in which methods are running. And when drilled down, you can see the method names and also the system libraries if you have opted to see.
Call Tree: A filter which helps you in detecting actual time constraints from methods.
Let us see, how we can alter the logic by using Time Profiler in actual application. Here we will compare with a simple for loop and while loop case to identify which one actually executes faster. We are not considering the resource usage in this example.
One with For loop:
private func testWithForLoop() {
for count in0..<10000 {
printMe(with: count)
}
}
One with While loop:
private func testWithWhileLoop() {
var count:Int=0
while count !=10000 {
printMe(with: count)
count +=1
}
}
Post running Profiler, we can see the results as shown below:
Filters used in Call tree to obtain above results were.
To infer from image, while loop took around 7.00ms and for loop as 6.00ms. We would now select the for loop as it works great.
FYI, this is just an example to show the time constraints what we actually need to look for. If you consider about resource allocations and memory used, Time profiler cannot be a helpful tool to analyse it.
Example Time Profiler GitHub code can be seen HERE
It is obvious that a good iOS coder, will always profile with Time profiler to make sure application works best for them.
Good day.
Comments