一个C ++ $ C $的执行Ç每隔T毫秒内执行措施 [英] Performing measures within the execution of a c++ code every t milliseconds

查看:120
本文介绍了一个C ++ $ C $的执行Ç每隔T毫秒内执行措施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个while循环和函数排序如下:

Given a while loop and the function ordering as follows:

int k=0;
int total=100;
while(k<total){
   doSomething();
   if(approx. t milliseconds elapsed) { measure(); }
   ++k;
}

我要执行'措施'每t个毫秒。但是,由于DoSomething的'可以接近从上一次执行第t个毫秒,这是可以接受的后约,执行t从上次测量经过毫秒的措施。
我的问题是:如何才能实现这一目标?

I want to perform 'measure' every t-th milliseconds. However, since 'doSomething' can be close to the t-th millisecond from the last execution, it is acceptable to perform the measure after approximately t milliseconds elapsed from the last measure. My question is: how could this be achieved?

一个解决办法是定时器设置为零,而每个DoSomething的后测量。当它被withing可接受范围,我执行措施,并复位。不过,我不是我应该用这样一个任务,C ++函数。我所看到的,也有一定的功能,但在争论哪一个是最合适的是我的理解之外。请注意,某些功能确实考虑到一些其他进程所需的时间,但我想我的定时器只能衡量我的C ++ code的执行时间(我希望这是显而易见的)。另一件事是测量的分辨率,如下面指出的。假设这些建议的媒介选择。

One solution would be to set timer to zero, and measure it after every 'doSomething'. When it is withing the acceptable range, I perform measures, and reset. However, I'm not which c++ function I should use for such a task. As I can see, there are certain functions, but the debate on which one is the most appropriate is outside of my understanding. Note that some of the functions actually take into account the time taken by some other processes, but I want my timer to only measure the time of the execution of my c++ code (I hope that is clear). Another thing is the resolution of the measurements, as pointed out below. Suppose the medium option of those suggested.

推荐答案

高分辨率时间是特定的平台,你有没有在问题中指定。标准库时钟()函数返回每秒递增在CLOCKS_PER_SEC计数。在某些平台上,这可能是速度不够快,给你你需要的决议,但你应该检查你的系统的节拍率,因为它是实现定义。然而,如果你发现这是足够高的话:

High resolution timing is platform specific, and you have not specified in the question. The standard library clock() function returns a count that increments at CLOCKS_PER_SEC per second. On some platforms this may be fast enough to give you the resolution you need but you should check your system's tick rate since it is implementation defined. However if you find it is high enough then:

#define SAMPLE_PERIOD_MS 100
#define SAMPLE_PERIOD_TICKS ((CLOCKS_PER_SEC * SAMPLE_PERIOD_MS) / 1000)

int k=0;
int total=100;
clock_t measure_time = clock() + SAMPLE_PERIOD_TICKS  ;
while(k<total)
{
   doSomething();
   if( clock() - measure_time > 0 )
   { 
        measure(); 
        measure_time += SAMPLE_PERIOD_TICKS ;
        ++k;
   }
}

您可能取代一些其他的高分辨率的时钟源,如果所需的时钟()。

You might replace clock() with some other high-resolution clock source if necessary.

然而要注意的几个问题。这种方法是一个忙环;除非任何DoSomething的()或测量()出CPU,这个过程将采取一切CPU周期就可以了。如果这是对一个目标的唯一code运行时,可能没有关系。另一方面是这是在通用OS例如Windows或Linux哪些不是实时运行的,该过程可以是$ P $对 - 抢占其他进程,而这可能影响取样周期的准确性。如果你需要精确定时使用RTOS和执行DoSomething的()和措施()在单独的线程会更好。即使在GPO时,效果会更好。例如,一个通用模式(使用德没有任何规范的捏造API)将是:

However note a couple of issues. This method is a "busy-loop"; unless either doSomething() or measure() yield the CPU, the process will take all the cpu cycles it can. If this is the only code running on a target, that may not matter. On the other hand is this is running on a general purpose OS such as Windows or Linux which are not real-time, the process may be pre-empted by other processes, and this may affect the accuracy of the sampling periodicity. If you need accurate timing use of an RTOS and performing doSomething() and measure() in separate threads would be better. Even in a GPOS that would be better. For example a general pattern (using a made-up API in teh absence of any specification) would be:

int main()
{
    StartThread( measure_thread, HIGH_PRIORITY ) ;
    for(;;)
    {
        doSomething() ;
    }
}

void measure_thread()
{
    for(;;)
    {
        measure() ;
        sleep( SAMPLE_PERIOD_MS ) ;
    }
}

在code为 measure_thread()只有准确,如果措施()需要一个可以忽略不计的时间来跑。如果需要显著时候你可能需要考虑这一点。如果它是不确定性,你甚至可以自己去衡量,以减去它的睡眠阶段的执行时间。

The code for measure_thread() is only accurate if measure() takes a negligible time to run. If it takes significant time you may need to account for that. If it is non-deterministic, you may even have to measure its execution time in order to subtract it the sleep period.

这篇关于一个C ++ $ C $的执行Ç每隔T毫秒内执行措施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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