views:

84

answers:

1

Hi: In an iPhone application I use a library(C++) which asynchronously makes a callback when computation is finished. Now I want to measure the time which is spent -including the method which calls the library- until the callback is made. Are there any possibilities to do this with the Instruments application from Apple? What are the best practices?

+1  A: 

In the past I have used the following for making network calls I had to optimize - although at first it seems a bit convoluted, it certainly gives the most accurate times I have seen.

uint64_t time_a = mach_absolute_time();

// do stuff

uint64_t time_b = mach_absolute_time();

[self logTime:(time_b-time_a)];

- (void) logTime:(uint64_t)machTime {
    static double timeScaleSeconds = 0.0;
    if (timeScaleSeconds == 0.0) {
        mach_timebase_info_data_t timebaseInfo;
        if (mach_timebase_info(&timebaseInfo) == KERN_SUCCESS) {
            double timeScaleMicroSeconds = ((double) timebaseInfo.numer / (double) timebaseInfo.denom) / 1000;
            timeScaleSeconds = timeScaleMicroSeconds / 1000000;
        }
    }

    NSLog(@"%g seconds", timeScaleSeconds*machTime);
}
adam