测量库调用和回调之间的时间

Sne*_*ney 2 c++ iphone performance objective-c instruments

嗨:在iPhone应用程序中,我使用一个库(C++),它在计算完成时异步进行回调.现在我想测量花费的时间 - 包括调用库的方法 - 直到进行回调.有没有可能使用Apple的Instruments应用程序执行此操作?什么是最佳做法?

ada*_*dam 8

在过去,我使用以下内容进行网络调用,我必须进行优化 - 虽然起初看起来有点复杂,但它确实给出了我见过的最准确的时间.

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);
}
Run Code Online (Sandbox Code Playgroud)