测量库调用和回调之间的时间 [英] Measure time between library call and callback

查看:129
本文介绍了测量库调用和回调之间的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

过去我使用以下方法进行网络调用我必须优化 - 尽管在首先它似乎有点复杂,它肯定给出了我见过的最准确的时间。

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);
}

这篇关于测量库调用和回调之间的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆