简体   繁体   中英

Using XCode and instruments to improve iPhone app performance

I've been experimenting with Instruments off and on for a while and and I still can't do the following (with any sensible results): determine or estimate the average runtime of a function that's called many times.

For example if I'm driving my gameLoop at 60 Hz with a CADisplayLink I'd like to see how long the loop takes to run on average... 10 ms? 30 ms etc.

I've come close with the "CPU activity" instrument but the results are inconsistent or don't make sense. The time profiler seems promising but all I can get is "% of runtime"... and I'd like an actual runtime.

I'm not sure instruments is your best bet here. Instruments samples your code, meaning (amongst other things) that it affects timing. If you want to know how long a loop takes then you should put something in the code to calculate and either retain or display the amount of time each loop is taking.

An actual runtime can be had by:

// activity starts
NSDate* startTime = [NSDate date];

// activity ends
NSLog(@"time elapsed this loop is %f", fabs([startTime timeIntervalSinceNow]));

Getting an average is easy if you have a time calculated per loop since you can sum the times, count the loops and divide before you display the average.

Anything in your loop affects the timing, of course, but this affects in a minimal way.

I've tried Time Profiler but it doesn't give you an average or total run time for a method. It just tells you how much time your code is spending in a method during your recording. It tells you your code spends a lot of time in a method but doesn't tell you whether that's because that method ran once and took a long time or whether that method was called many times but ran real fast. If its the former then you should concentrate on making the method faster. If it's the latter you should have your code call the method less often. You have to figure out which it is yourself. Making things more difficult is the Run Loop which has input sources that fire off events like the timer service or I/O. These get passed to handlers which could be Apple's. If one of Apple's handlers is taking a long time then Time Profiler shows it as a hot spot but unless you know what that handler is doing and why there's not much you can do about it. Apple's stuff is not even necessarily called anywhere from your end of the call stack so you can't optimize very easily. I've had more success with OpenGL ES Analyzer as its API Statistics gives you call count, total time, and average time for OpenGL ES calls. Doesn't help you if you aren't doing OpenGL ES.

您应该尝试使用Shark进行您所说的时间分析。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM