获得从时钟周期时间 [英] obtaining time from clock cycles

查看:255
本文介绍了获得从时钟周期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行QNX,

我用一个函数来获取每秒的时钟周期,

I used a function to get clock cycles per second,

uint64_t clockPerSec = getCPS();
uint64_t currentClockCycle = getCurrentCycle();

功能

uint64_t getCPS()
{
   return (~(uint64_t)0) /SYSPAGE_ENTRY(qtime) -> cycles_per_sec;
}

uint63_t getCurrentCycle()
{
   return ClockCycles();
}

然后运行一个函数之后

then after running a function

currentClockCycle = getCurrentCycle() - currentClockCycle;

我不使用它在整个应用程序,所以我没有时钟的超支/溢出,只是衡量一些补充/更改后,一个功能的性能。

I am not using it through the whole applications, so I dont have overruns/overflow of the clock, just to measure one function performance after some additions/changes.

反正, 我只是想知道如果我得到正确的输出。

anyway, I am just wondering if I am getting the right output.

我计算的结果是这样,

double result = static_cast<double>(clockPerSec)/currentClockCycle;
// this get me the time in second??
// then multiplied it by 1000000 to get a micro-sec measurement 

我是做错了什么?

am I doing anything wrong?

在使用

ftime(&t_start);

然后

ftime(&t_end);

和输出的区别就这样,我看到的时候,我得到的是更大的,几乎两倍

and output the difference this way, I see that the time I get is bigger, almost twice

第一种方法,我得到0.6毫秒 使用FTIME第二个我得到的结果1.XX毫秒

first method I get 0.6 ms second one using ftime I get the result 1.xx ms

推荐答案

你只是混合(措施)两个不同的东西:时钟周期的是CPU的计数滴答内核使您的应用程序 - 它不计其他应用通过同一内核上运行。而 FTIME 返回一个绝对的(挂钟)时间。所以两者之间的差异 ftimes 返回两个时间点之间的绝对持续时间。而第一计数只是衡量有多少CPU蜱你(和你的唯一)的应用程序已经消耗...所以你可能会认为这是的完全你的应用程序运行时间的 - 也就是说,如果没有的<​​strong>其他在这台主机的应用程序,第一和第二次测量将是(理论上)相等(或左右)。

you just mix (measure) two different things: clock cycles is a count of CPU ticks kernel gives to your app -- it doesn't count other apps running by same kernel. while ftime returns an absolute (wall clock) time. so difference between two ftimes return an absolute time duration between two time points. while the first counts just measure how many CPU ticks your (and only your) app have consumed... so you may consider this as exclusively your app running time -- i.e. if there is no other apps at this host, first and second measurements will be (theoretically) equal (or so).

这篇关于获得从时钟周期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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