精确获取时间(QueryPerformanceCounter)
2012-12-19 23:48
309 查看
LARGE_INTEGERtima,timb;
QueryPerformanceCounter(&tima);
在WindowsServer2003和WindowsXP中使用QueryPerformanceCounter函数的程序可能执行不当
QueryPerformanceCounter來精確計算執行時間
QueryPerformanceCounter來精確計算執行時間
//這個程式展示了如何使用QueryPerformanceCounter來精確計算執行時間
//代码
LARGE_INTEGERm_liPerfFreq={0};
//获取每秒多少CPUPerformanceTick
QueryPerformanceFrequency(&m_liPerfFreq);
LARGE_INTEGERm_liPerfStart={0};
QueryPerformanceCounter(&m_liPerfStart);
for(inti=0;i<100;i++)
cout<<i<<endl;
LARGE_INTEGERliPerfNow={0};
//计算CPU运行到现在的时间
QueryPerformanceCounter(&liPerfNow);
inttime=(((liPerfNow.QuadPart-m_liPerfStart.QuadPart)*1000)/m_liPerfFreq.QuadPart);
charbuffer[100];
sprintf(buffer,"執行時間%dmillisecond",time);
cout<<buffer<<endl;
QueryPerformanceCounter()这个函数返回高精确度性能计数器的值,它可以以微妙为单位计时.但是QueryPerformanceCounter()确切的精确计时的最小单位是与系统有关的,所以,必须要查询系统以得到QueryPerformanceCounter()返回的嘀哒声的频率.
QueryPerformanceFrequency()提供了这个频率值,返回每秒嘀哒声的个数.
计算确切的时间是从第一次调用QueryPerformanceCounter()开始的
假设得到的LARGE_INTEGER为nStartCounter,过一段时间后再次调用该函数结束的,
设得到nStopCounter.
两者之差除以QueryPerformanceFrequency()的频率就是开始到结束之间的秒数.由于计时函数本身要耗费很少的时间,要减去一个很少的时间开销.但一般都把这个开销忽略.公式如下:
nStopCounter-nStartCounter
ElapsedTime=-------------------------------------overhead
frequency
doubletime=(nStopCounter.QuadPart-nStartCounter.QuadPart)/frequency.QuadPart
这两个函数是VC提供的仅供Windows95及其后续版本使用的精确时间函数,并要求计算机从硬件上支持精确定时器。
QueryPerformanceFrequency()函数和QueryPerformanceCounter()函数的原型如下:
数据类型ARGE_INTEGER既可以是一个8字节长的整型数,也可以是两个4字节长的整型数的联合结构,其具体用法根据编译器是否支持64位而定。该类型的定义如下:
在进行定时之前,先调用QueryPerformanceFrequency()函数获得机器内部定时器的时钟频率,然后在需要严格定时的事件发生之前和发生之后分别调用QueryPerformanceCounter()函数,利用两次获得的计数之差及时钟频率,计算出事件经历的精确时间。下列代码实现1ms的精确定时:
LARGE_INTEGERlitmp;
LONGLONGQPart1,QPart2;
doubledfMinus,dfFreq,dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq=(double)litmp.QuadPart;//获得计数器的时钟频率
QueryPerformanceCounter(&litmp);
QPart1=litmp.QuadPart;//获得初始值
do
{
QueryPerformanceCounter(&litmp);
QPart2=litmp.QuadPart;//获得中止值
dfMinus=(double)(QPart2-QPart1);
dfTim=dfMinus/dfFreq;//获得对应的时间值,单位为秒
}while(dfTim<0.001);
其定时误差不超过1微秒,精度与CPU等机器配置有关。下面的程序用来测试函数Sleep(100)的精确持续时间:
LARGE_INTEGERlitmp;
LONGLONGQPart1,QPart2;
doubledfMinus,dfFreq,dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq=(double)litmp.QuadPart;//获得计数器的时钟频率
QueryPerformanceCounter(&litmp);
QPart1=litmp.QuadPart;//获得初始值
Sleep(100);
QueryPerformanceCounter(&litmp);
QPart2=litmp.QuadPart;//获得中止值
dfMinus=(double)(QPart2-QPart1);
dfTim=dfMinus/dfFreq;//获得对应的时间值,单位为秒
由于Sleep()函数自身的误差,上述程序每次执行的结果都会有微小误差。下列代码实现1微秒的精确定时:
LARGE_INTEGERlitmp;
LONGLONGQPart1,QPart2;
doubledfMinus,dfFreq,dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq=(double)litmp.QuadPart;//获得计数器的时钟频率
QueryPerformanceCounter(&litmp);
QPart1=litmp.QuadPart;//获得初始值
do
{
QueryPerformanceCounter(&litmp);
QPart2=litmp.QuadPart;//获得中止值
dfMinus=(double)(QPart2-QPart1);
dfTim=dfMinus/dfFreq;//获得对应的时间值,单位为秒
}while(dfTim<0.000001);
其定时误差一般不超过0.5微秒,精度与CPU等机器配置有关。
来源:/article/2464923.html
QueryPerformanceCounter(&tima);
在WindowsServer2003和WindowsXP中使用QueryPerformanceCounter函数的程序可能执行不当
QueryPerformanceCounter來精確計算執行時間
QueryPerformanceCounter來精確計算執行時間
//這個程式展示了如何使用QueryPerformanceCounter來精確計算執行時間
//代码
LARGE_INTEGERm_liPerfFreq={0};
//获取每秒多少CPUPerformanceTick
QueryPerformanceFrequency(&m_liPerfFreq);
LARGE_INTEGERm_liPerfStart={0};
QueryPerformanceCounter(&m_liPerfStart);
for(inti=0;i<100;i++)
cout<<i<<endl;
LARGE_INTEGERliPerfNow={0};
//计算CPU运行到现在的时间
QueryPerformanceCounter(&liPerfNow);
inttime=(((liPerfNow.QuadPart-m_liPerfStart.QuadPart)*1000)/m_liPerfFreq.QuadPart);
charbuffer[100];
sprintf(buffer,"執行時間%dmillisecond",time);
cout<<buffer<<endl;
QueryPerformanceCounter()这个函数返回高精确度性能计数器的值,它可以以微妙为单位计时.但是QueryPerformanceCounter()确切的精确计时的最小单位是与系统有关的,所以,必须要查询系统以得到QueryPerformanceCounter()返回的嘀哒声的频率.
QueryPerformanceFrequency()提供了这个频率值,返回每秒嘀哒声的个数.
计算确切的时间是从第一次调用QueryPerformanceCounter()开始的
假设得到的LARGE_INTEGER为nStartCounter,过一段时间后再次调用该函数结束的,
设得到nStopCounter.
两者之差除以QueryPerformanceFrequency()的频率就是开始到结束之间的秒数.由于计时函数本身要耗费很少的时间,要减去一个很少的时间开销.但一般都把这个开销忽略.公式如下:
nStopCounter-nStartCounter
ElapsedTime=-------------------------------------overhead
frequency
doubletime=(nStopCounter.QuadPart-nStartCounter.QuadPart)/frequency.QuadPart
这两个函数是VC提供的仅供Windows95及其后续版本使用的精确时间函数,并要求计算机从硬件上支持精确定时器。
QueryPerformanceFrequency()函数和QueryPerformanceCounter()函数的原型如下:
BOOLQueryPerformanceFrequency(LARGE_INTEGER*lpFrequency); BOOLQueryPerformanceCounter(LARGE_INTEGER*lpCount);
数据类型ARGE_INTEGER既可以是一个8字节长的整型数,也可以是两个4字节长的整型数的联合结构,其具体用法根据编译器是否支持64位而定。该类型的定义如下:
typedefunion_LARGE_INTEGER { struct { DWORDLowPart;//4字节整型数 LONGHighPart;//4字节整型数 }; LONGLONGQuadPart;//8字节整型数 }LARGE_INTEGER;
在进行定时之前,先调用QueryPerformanceFrequency()函数获得机器内部定时器的时钟频率,然后在需要严格定时的事件发生之前和发生之后分别调用QueryPerformanceCounter()函数,利用两次获得的计数之差及时钟频率,计算出事件经历的精确时间。下列代码实现1ms的精确定时:
LARGE_INTEGERlitmp;
LONGLONGQPart1,QPart2;
doubledfMinus,dfFreq,dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq=(double)litmp.QuadPart;//获得计数器的时钟频率
QueryPerformanceCounter(&litmp);
QPart1=litmp.QuadPart;//获得初始值
do
{
QueryPerformanceCounter(&litmp);
QPart2=litmp.QuadPart;//获得中止值
dfMinus=(double)(QPart2-QPart1);
dfTim=dfMinus/dfFreq;//获得对应的时间值,单位为秒
}while(dfTim<0.001);
其定时误差不超过1微秒,精度与CPU等机器配置有关。下面的程序用来测试函数Sleep(100)的精确持续时间:
LARGE_INTEGERlitmp;
LONGLONGQPart1,QPart2;
doubledfMinus,dfFreq,dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq=(double)litmp.QuadPart;//获得计数器的时钟频率
QueryPerformanceCounter(&litmp);
QPart1=litmp.QuadPart;//获得初始值
Sleep(100);
QueryPerformanceCounter(&litmp);
QPart2=litmp.QuadPart;//获得中止值
dfMinus=(double)(QPart2-QPart1);
dfTim=dfMinus/dfFreq;//获得对应的时间值,单位为秒
由于Sleep()函数自身的误差,上述程序每次执行的结果都会有微小误差。下列代码实现1微秒的精确定时:
LARGE_INTEGERlitmp;
LONGLONGQPart1,QPart2;
doubledfMinus,dfFreq,dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq=(double)litmp.QuadPart;//获得计数器的时钟频率
QueryPerformanceCounter(&litmp);
QPart1=litmp.QuadPart;//获得初始值
do
{
QueryPerformanceCounter(&litmp);
QPart2=litmp.QuadPart;//获得中止值
dfMinus=(double)(QPart2-QPart1);
dfTim=dfMinus/dfFreq;//获得对应的时间值,单位为秒
}while(dfTim<0.000001);
其定时误差一般不超过0.5微秒,精度与CPU等机器配置有关。
来源:
相关文章推荐
- 精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)测试程序效率
- 精确获取时间(QueryPerformanceCounter)
- 获取系统时间(精确到微秒),驱动中KeQueryPerformanceCounter,Win32程序QueryPerformanceCounter
- 精确获取时间:QueryPerformanceFrequency()和QueryPerformanceCounter()
- 精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)
- C++精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)
- 如何精确计算程序运行时间——精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)
- 精确获取时间(QueryPerformanceCounter)