WIN32微秒级计时方法

来源:互联网 发布:mac制作铃声的教程 编辑:程序博客网 时间:2024/06/05 20:57

#include "stdafx.h"
#include <iostream>
#include <crtdbg.h>
#include <sys/timeb.h>
#include <time.h>
#include <windows.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 LARGE_INTEGER startCount;
 LARGE_INTEGER endCount;
 LARGE_INTEGER freq;

 QueryPerformanceFrequency(&freq);
 QueryPerformanceCounter(&startCount);

 //////////////////////////////////////////////////////////////////////////
 ///精确测试时间
 //////////////////////////////////////////////////////////////////////////
 Sleep(3500);

 QueryPerformanceCounter(&endCount);
 double elapsed = (double)(endCount.QuadPart - startCount.QuadPart) / freq.QuadPart;
 cout << "Total time elapsed : " << elapsed << endl;

 system("pause");

 return 0;
}

---------------------------------------------------------------------------------------
QueryPerformanceFrequency() - 基本介绍

类型:Win32API

原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

作用:返回硬件支持的高精度计数器的频率。

返回值:非零,硬件支持高精度计数器;零,硬件不支持,读取失败。

0 0
原创粉丝点击