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

发布于 2024-08-29 16:34:54 字数 130 浏览 4 评论 0原文

你好:在 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生漫 2024-09-05 16:34:54

过去,我使用以下方法进行网络调用,我必须对其进行优化 - 尽管乍一看似乎有点复杂,但它确实给出了我所见过的最准确的时间。

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

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);
}
对不⑦ 2024-09-05 16:34:54

我编写了一个 C++ 类来包装 mach_absolute_time 调用。这使得在事件开始/停止时添加代码以测量时间差变得很方便。

如果您想在类中使用它来进行计时(在调用之间)或基于状态的行为(在计时器达到 X 后执行某些操作),它也可以很好地工作。

StopWatch.h

class StopWatch
{
private:
        uint64 _start;
        uint64 _stop;
        uint64 _elapsed;
public:
   void Start();
   void Stop();
   void Reset();
   void Continue();
   double GetSeconds();
};

StopWatch.cpp

#include "Stopwatch.h"
#include <mach/mach_time.h>


void StopWatch::Start()
{
        _stop = 0;
        _elapsed = 0;
        _start = mach_absolute_time();
}

void StopWatch::Stop()
{
        _stop = mach_absolute_time();
   if(_start > 0)
   {
      if(_stop > _start)
      {
         _elapsed = _stop - _start;
      }
   }
}

void StopWatch::Reset()
{
   _start = 0;
   _stop  = 0;
   _elapsed = 0;
}

void StopWatch::Continue()
{
   _elapsed = 0;
   _stop = 0;
}

double StopWatch::GetSeconds()
{
   double elapsedSeconds = 0.0;
   uint64 elapsedTimeNano = 0;

        if(_elapsed > 0)
        {  // Stopped
                mach_timebase_info_data_t timeBaseInfo;
                mach_timebase_info(&timeBaseInfo);
                elapsedTimeNano = _elapsed * timeBaseInfo.numer / timeBaseInfo.denom;
                elapsedSeconds = elapsedTimeNano * 1.0E-9;
        }
        else if(_start > 0)
        {  // Running or Continued
      uint64_t elapsedTemp;
                uint64_t stopTemp = mach_absolute_time();
      if(stopTemp > _start)
      {
         elapsedTemp = stopTemp - _start;
      }
      else
      {
         elapsedTemp = 0;
      }
                mach_timebase_info_data_t timeBaseInfo;
                mach_timebase_info(&timeBaseInfo);
                elapsedTimeNano = elapsedTemp * timeBaseInfo.numer / timeBaseInfo.denom;
                elapsedSeconds = elapsedTimeNano * 1.0E-9;
        }
   return elapsedSeconds;
}

I have written a c++ class to wrap the mach_absolute_time calls. This makes it handy to sprinkle in the code at the start/stop of events for measuring time difference.

It also works well if you want to use it in a class to do timing (between calls) or for state based behavior (do something after the timer reaches X).

StopWatch.h

class StopWatch
{
private:
        uint64 _start;
        uint64 _stop;
        uint64 _elapsed;
public:
   void Start();
   void Stop();
   void Reset();
   void Continue();
   double GetSeconds();
};

StopWatch.cpp

#include "Stopwatch.h"
#include <mach/mach_time.h>


void StopWatch::Start()
{
        _stop = 0;
        _elapsed = 0;
        _start = mach_absolute_time();
}

void StopWatch::Stop()
{
        _stop = mach_absolute_time();
   if(_start > 0)
   {
      if(_stop > _start)
      {
         _elapsed = _stop - _start;
      }
   }
}

void StopWatch::Reset()
{
   _start = 0;
   _stop  = 0;
   _elapsed = 0;
}

void StopWatch::Continue()
{
   _elapsed = 0;
   _stop = 0;
}

double StopWatch::GetSeconds()
{
   double elapsedSeconds = 0.0;
   uint64 elapsedTimeNano = 0;

        if(_elapsed > 0)
        {  // Stopped
                mach_timebase_info_data_t timeBaseInfo;
                mach_timebase_info(&timeBaseInfo);
                elapsedTimeNano = _elapsed * timeBaseInfo.numer / timeBaseInfo.denom;
                elapsedSeconds = elapsedTimeNano * 1.0E-9;
        }
        else if(_start > 0)
        {  // Running or Continued
      uint64_t elapsedTemp;
                uint64_t stopTemp = mach_absolute_time();
      if(stopTemp > _start)
      {
         elapsedTemp = stopTemp - _start;
      }
      else
      {
         elapsedTemp = 0;
      }
                mach_timebase_info_data_t timeBaseInfo;
                mach_timebase_info(&timeBaseInfo);
                elapsedTimeNano = elapsedTemp * timeBaseInfo.numer / timeBaseInfo.denom;
                elapsedSeconds = elapsedTimeNano * 1.0E-9;
        }
   return elapsedSeconds;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文