Search code examples
iphonexcodeprofilinginstruments

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.


Solution

  • 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.