本文主要是介绍C语言实现高精度计时和高精度延时微秒级别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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; /* 开始计数值us */endCounter = endTick.QuadPart * 1000000 / countFQR.QuadPart; /* 结束计数值us */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;//usprintf("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:10000000HZ start:430589299158 end:430589305471 CostTimes:631.300us
startCounter:43058929915 endCounter:43058930547 CostTimes:632usHigh Precision Delay Demo:
start:430589306518 end:430591306519 CostTimes:200000usCompiler Date : Mar 21 2024 20:12:07
这篇关于C语言实现高精度计时和高精度延时微秒级别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!