赞助

C++高精度计算代码运行时间(转载)

转载:http://blog.csdn.net/rrrfff/article/details/6583410

 1     //在定时前应该先调用QueryPerformanceFrequency()函数获得机器内部计时器的时钟频率.
 2     //接着在需要严格计时的事件发生前和发生之后分别调用QueryPerformanceCounter(),利用两次获得的计数之差和时钟频率,就可以计算出事件经历的精确时间,
 3     //供WIN9X使用的高精度定时器:QueryPerformanceFrequency()和QueryPerformanceCounter(),要求计算机从硬件上支持高精度定时器.
 4     //数据类型LARGEINTEGER既可以是一个作为8字节长的整数,也可以是作为两个4字节长的整数的联合结构,其具体用法根据编译器是否支持64位而定.
 5     LARGE_INTEGER Frequency = {0};
 6     QueryPerformanceFrequency(&Frequency);
 7     double F    = (double)Frequency.QuadPart;
 8     QueryPerformanceCounter(&Frequency);
 9     LONGLONG I  = Frequency.QuadPart;
10     {
11         //do sth here
12     }
13     QueryPerformanceCounter(&Frequency);
14     CString result;
15     result.Format(_T("Result: %f"), double(Frequency.QuadPart - I) / F);
16     MessageBox(NULL, result, _T("测试报告"), MB_OK);

 

posted @ 2016-11-14 11:25  车臣  阅读(1437)  评论(0编辑  收藏  举报