C GetTickCount(windows函数)到时间(纳秒)

发布于 2024-09-25 05:50:04 字数 195 浏览 0 评论 0原文

我正在测试同事提供的一段代码,我需要测量一个例程的执行时间,而不是执行(线程)上下文切换。

测量时间的最佳选择是什么?我知道可以使用高分辨率计时器,

QueryPerformanceCounter
QueryPerformanceFrequency

但我如何使用该计时器将其转换为毫秒或纳秒?

I'm testing one code provided from my colleague and I need to measure the time of execution of one routine than performs a context switch (of threads).

What's the best choice to measure the time? I know that is available High Resolution Timers like,

QueryPerformanceCounter
QueryPerformanceFrequency

but how can I translate using that timers to miliseconds or nanoseconds?

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

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

发布评论

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

评论(2

┊风居住的梦幻卍 2024-10-02 05:50:04
LARGE_INTEGER lFreq, lStart;
LARGE_INTEGER lEnd;
double d;

QueryPerformanceFrequency(&lFreq);
QueryPerformanceCounter(&lStart);

/* do something ... */

QueryPerformanceCounter(&lEnd);
d = ((doublel)End.QuadPart - (doublel)lStart.QuadPart) / (doublel)lFreq.QuadPart;

d 是以秒为单位的时间间隔。

LARGE_INTEGER lFreq, lStart;
LARGE_INTEGER lEnd;
double d;

QueryPerformanceFrequency(&lFreq);
QueryPerformanceCounter(&lStart);

/* do something ... */

QueryPerformanceCounter(&lEnd);
d = ((doublel)End.QuadPart - (doublel)lStart.QuadPart) / (doublel)lFreq.QuadPart;

d is time interval in seconds.

旧城空念 2024-10-02 05:50:04

由于我正在执行的操作约为 500 纳秒,并且计时器没有精度,所以我所做的是,

我使用 GetTickCount() 保存了实际时间 - (使用 ~ 12milis 的精度)并执行路线 N_TIMES (例程执行的次数)比保留,直到我在控制台上按下某些内容。

再次计算时间,将差值除以N_TIMES,类似这样:

int static counter;

无效例程()
{
// 这里的操作..
计数器++;
}

int main(){
开始 <- GetTickCount();
句柄 <- createThread(....., 例程,...);
恢复线程(句柄);

getchar();

WaitForSingleObject(句柄,无限);

已用时间 = (GetTickCount() - 开始) * 1000000.0) / 计数器;
printf("纳诺数:%d",已过去);
}

:)

As the operation than i am executing is in order of 500 nanos, and the timers doens't have precision, what i made was,

i saved actual time with GetTickCount() - (Uses precision of ~ 12milis) and performs the execution of a route N_TIMES (Number of times than routine executed) than remains until i press something on console.

Calculate the time again, and make the difference dividing by N_TIMES, something like that:

int static counter;

void routine()
{
// Operations here..
counter++;
}

int main(){
start <- GetTickCount();
handle <- createThread(....., routine,...);
resumeThread(handle);

getchar();

WaitForSingleObject(handle, INFINITE);

Elapsed = (GetTickCount() - start) * 1000000.0) / counter;
printf("Nanos: %d", elapsed);
}

:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文