C_C++ 各种计时函数
来源:互联网 发布:java安装方法 编辑:程序博客网 时间:2024/06/08 01:06
对Windows平台下常用的计时函数进行总结,包括精度为秒、毫秒、微秒三种精度的5 种方法。分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及clock()不仅可以用在Windows 系统,也可以用于Linux系统。
在Windows系统下三种,使用Windows提供的API接口timeGetTime()、 GetTickCount()及QueryPerformanceCounter()来完成。
#include <windows.h> #include <time.h> //time_t time() clock_t clock() #include <Mmsystem.h> //timeGetTime() #pragma comment(lib, "Winmm.lib") //timeGetTime() int main(int argc, char* argv[]){ //用time()来计时 秒 time_t timeBegin, timeEnd; timeBegin = time(NULL); Sleep(800); timeEnd = time(NULL); printf("time %d\n", timeEnd - timeBegin); //用clock()来计时 毫秒 clock_t clockBegin, clockEnd; clockBegin = clock(); Sleep(800); clockEnd = clock(); printf("clock %d\n", clockEnd - clockBegin); //用timeGetTime()来计时 毫秒 DWORD dwBegin, dwEnd; dwBegin = timeGetTime(); Sleep(800); dwEnd = timeGetTime(); printf("timeGetTime %d\n", dwEnd - dwBegin); //用GetTickCount()来计时 毫秒 DWORD dwGTCBegin, dwGTCEnd; dwGTCBegin = GetTickCount(); Sleep(800); dwGTCEnd = GetTickCount(); printf("GetTickCount %d\n", dwGTCEnd - dwGTCBegin); //用QueryPerformanceCounter()来计时 微秒 LARGE_INTEGER large_interger; double dff; __int64 c1, c2; //获取是否支持精确定时器。 定时器的频率 if( QueryPerformanceFrequency(&large_interger) ) { dff = large_interger.QuadPart; //获取定时器的值 QueryPerformanceCounter(&large_interger); c1 = large_interger.QuadPart; Sleep(800); //获取定时器的值 QueryPerformanceCounter(&large_interger); c2 = large_interger.QuadPart; printf("本机高精度计时器频率%lf\n", dff); printf("第一次计时器值%I64d 第二次计时器值%I64d 计时器差%I64d\n", c1, c2, c2 - c1); printf("计时%lf毫秒\n", (c2 - c1) * 1000 / dff); printf("\n"); } return 0; }
- C_C++ 各种计时函数
- C_C++ 各种计时函数
- C_C++ 各种计时函数
- C_C++ 各种计时函数
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- C/C++各种计时函数
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- windows各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- 知道二叉树的前序遍历和中序遍历重构二叉树
- App列表之圆角ListView
- Eclipse中代码规范化(ctrl+shift+f)快捷键不能用
- C#实现窗口渐隐,并启动另外个窗口
- 数据结构学习之回溯法求解八皇后问题
- C_C++ 各种计时函数
- Linux下ARM汇编语法
- hibernate.dialect' must be set when no Connection available错误
- Unable to get the default Bean Validation factory
- 数据结构学习之启发式搜索求解骑士周游问题
- sgu 125 Shtirlits
- oracle 11g 学习笔记 10_31(2)_维护数据的完整性
- CSS选择器的浏览器支持大全
- OpenGL 4.3 (Core Profile) - August 6, 2012 spec Fundamental 2.5 Objects and the Object Model