如何在windows下面实现小于1毫秒的精确计时
如何在windows下面实现小于1毫秒的精确计时?请指教 问题点数:50、回复次数:6Top
1 楼z_sky()回复于 2002-05-24 15:56:15 得分 10
Multimedia Timer Functions
The following functions are used with multimedia timers.
timeBeginPeriod
timeEndPeriod
timeGetDevCaps
timeGetSystemTime
timeGetTime
timeKillEvent
TimeProc
timeSetEvent
check them with MSDNTop
2 楼masterz(www.fruitfruit.com)回复于 2002-05-24 16:41:07 得分 10
__int64 RDTSC()
{
unsigned int lopart, hipart;
__asm
{
rdtsc
mov lopart, eax
mov hipart, edx
}
return lopart + (hipart*(4294967296UL));
}
Top
3 楼dylanwolf()回复于 2002-05-24 16:48:51 得分 10
Use this two functions, can achieve 微秒级别
QueryPerformanceCounter
The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter, if one exists.
QueryPerformanceFrequency
The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists.
BOOL QueryPerformanceFrequency(
LARGE_INTEGER *lpFrequency // address of current frequency
);
Parameters
lpFrequency
Pointer to a variable that the function sets, in counts per second, to the current performance-counter frequency. If the installed hardware does not support a high-resolution performance counter, this parameter can be to zero.
BOOL QueryPerformanceCounter(
LARGE_INTEGER *lpPerformanceCount // pointer to counter value
);
Parameters
lpPerformanceCount
Pointer to a variable that the function sets, in counts, to the current performance-counter value. If the installed hardware does not support a high-resolution performance counter, this parameter can be to zero.
Top
4 楼winne_ll(feiyang)回复于 2002-05-24 16:54:36 得分 20
可以精确到0.000001秒
看看下面的代码,你在VC里运行试试:)
#include "stdafx.h"
#include "windows.h"
int main(int argc, char* argv[])
{
LARGE_INTEGER countstart;
LARGE_INTEGER countend;
__int64 timediff;
LARGE_INTEGER frequent;
QueryPerformanceCounter( &countstart);
printf("Hello World!\n");
printf("Hello World!\n");
printf("Hello World!\n");
QueryPerformanceCounter( &countend);
timediff = (__int64)countend.QuadPart-(__int64)countstart.QuadPart;
QueryPerformanceFrequency(&frequent);
double dsec = (double)timediff/(double)frequent.QuadPart;
printf("elapsed time :%f(second)\n",dsec);
return 0;
}
也可以用下面的
可以使用多媒体定时器啊,函数如下:
1。QueryPerformanceFrequency,取得你的系统支持的定时器的频率
2。QueryPerformanceCounter ,计算走过的时间用,这个时间指的是系统启动后经历的时间,你自己程序的时间要用减法来算。Top
5 楼1980xls(狮子山人)回复于 2002-05-24 19:57:44 得分 0
getthinckcount()Top
6 楼programcat2001(旧游以梦)回复于 2002-05-24 19:58:53 得分 0
用工业计时器Top




