C语言实现高精度计时和高精度延时微秒级别
目的说明 环境说明 一、高精度延时(微秒级别) 二、测试例程 三、测试结果
目的说明
在Windows下C语言实现高精度计时功能和高精度延时微秒级别
环境说明
Dev- C++ V5. 11
一、高精度延时(微秒级别)
void vDelayUS ( u32 usDelay)
{
LARGE_INTEGER startTick = { 0 } , curTick = { 0 } , countFQR = { 0 } ;
u64 delayTick = 0 ;
QueryPerformanceFrequency ( & countFQR) ;
delayTick = ( countFQR. QuadPart * usDelay) / 1000000 ;
QueryPerformanceCounter ( & startTick) ;
while ( 1 )
{
QueryPerformanceCounter ( & curTick) ;
if ( ( ( u64) ( curTick. QuadPart - startTick. QuadPart) ) >= delayTick)
{
break ;
}
}
}
二、测试例程
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <windows.h>
int main ( int argc, char * argv[ ] )
{
LARGE_INTEGER startTick = { 0 } , endTick = { 0 } , countFQR = { 0 } ;
u64 startCounter = 0 , endCounter = 0 ;
u64 absTime = 0 ;
double costTime = 0 ;
QueryPerformanceFrequency ( & countFQR) ;
QueryPerformanceCounter ( & startTick) ;
vOrgData_Handler ( ) ;
QueryPerformanceCounter ( & endTick) ;
startCounter = startTick. QuadPart * 1000000 / countFQR. QuadPart;
endCounter = endTick. QuadPart * 1000000 / countFQR. QuadPart;
printf ( "\r\n\r\n" ) ;
printf ( "High Precision Timing Demo:\r\n" ) ;
costTime = ( double ) ( endTick. QuadPart - startTick. QuadPart) * 1000000.0 / countFQR. QuadPart;
printf ( "countFQR:%lldHZ start:%lld end:%lld CostTimes:%0.3fus\r\n" , countFQR, startTick. QuadPart, endTick. QuadPart, costTime) ;
printf ( "startCounter:%lld endCounter:%lld CostTimes:%uus\r\n" , startCounter, endCounter, ( endCounter - startCounter) ) ;
QueryPerformanceCounter ( & startTick) ;
vDelayUS ( 200000 ) ;
QueryPerformanceCounter ( & endTick) ;
printf ( "\r\n\r\n" ) ;
printf ( "High Precision Delay Demo:\r\n" ) ;
absTime = ( endTick. QuadPart - startTick. QuadPart) * 1000000 / countFQR. QuadPart;
printf ( "start:%lld end:%lld CostTimes:%lldus\r\n\r\n" , startTick. QuadPart, endTick. QuadPart, absTime) ;
printf ( "Compiler Date : %s %s\r\n" , __DATE__ , __TIME__ ) ;
while ( 1 ) ;
return 0 ;
}
三、测试结果
High Precision Timing Demo:
countFQR: 10000000 HZ start: 430589299158 end: 430589305471 CostTimes: 631.300u s
startCounter: 43058929915 endCounter: 43058930547 CostTimes: 632u s
High Precision Delay Demo:
start: 430589306518 end: 430591306519 CostTimes: 200000u s
Compiler Date : Mar 21 2024 20 : 12 : 07