C++高精度性能测试函数
2015-07-08 14:44
399 查看
在实际software开发工作中,我们经常会测试某个module或者function的执行效率。或者是某个算法的时间复杂度(虽然时间复杂度一定程度上依赖于机器性能,但在同一台computer上,经过算法优化,可以测试其复杂度);这时候就需要精确获取时间,才可以准确的运行时间,下面的函数实现了精确计时,计时精度可以达到微秒级;可用于测试某个模块的效率!
//Purpose :this programme is designed for testing the performance of your code ,some function ect, //it can show the time of spending on your running on it . it is beneficial to improve your performance //of code // author :Jackery_shh // data: July 8th 2015 #include<Windows.h> #include<iostream> #include<tchar.h> using namespace std; #define IN #ifdef _DEBUG #define _Trace_Size 500 inline void FileTrace(IN LPCTSTR szFormat, ...) { if (szFormat == NULL) return; TCHAR t_trace[_Trace_Size] = { _T('\0') }; va_list vl = NULL; // 生成内容 va_start(vl, szFormat); _vsntprintf_s(t_trace, _Trace_Size, _TRUNCATE, szFormat, vl); va_end(vl); OutputDebugString(t_trace); } #else inline void FileTrace(IN LPCTSTR szFormat, ...){} #endif LARGE_INTEGER begin_time ,freq; int main() { LARGE_INTEGER begin_time ,freq; QueryPerformanceFrequency (&freq); QueryPerformanceCounter (&begin_time); //begin time for (long test0=0;test0<5000000;test0++) { test0=test0+1; } LARGE_INTEGER end_time0; QueryPerformanceCounter (&end_time0); //after runnin the 'for' loop statement,the time is Time0 double Time0 = ((double) (end_time0.QuadPart - begin_time.QuadPart)*1000.0) / (double) (freq.QuadPart); int test1=10000; while (test1) { test1--; } LARGE_INTEGER end_time1; QueryPerformanceCounter (&end_time1); double Time1 = ((double) (end_time1.QuadPart - begin_time.QuadPart)*1000.0) / (double) (freq.QuadPart); FileTrace(_T("*********************************************************************************\n")); FileTrace(_T("Test result is as follow :\ntime: %I64x, %.3fms, %.3fms\n"), (ULONGLONG)0, Time0, Time1); FileTrace(_T("*********************************************************************************\n")); system("pause"); return 0; }test result is :
相关文章推荐
- (C++)函数参数传递中的一级指针和二级指针
- C/C++ 字节序/位域(Bit-fields)之我见
- c与c++关键字的用法总结
- INI文件的读取(C语言:GetPrivateProfileString/GetPrivateProfileInt)
- 黑马-C语言-枚举
- C++继承方式
- c++链表std::list
- eclipse安装C++工程环境----CDT环境————eclipse开发openCV环境准备1
- 单通道和三通道学习理解单通道:
- C语言基础进阶之 MessageBox()用法简介
- 服务端收发登录注册流程
- 怎么将vim修改像vc++自动换行缩进
- malloc函数学习
- C/C++校招笔试面试经典题目总结一
- C++命名空间
- 加速c++输入
- C++ COM组件编写初探
- 虚函数的使用
- c++中构造函数 、析构函数的作用域详解
- C++对象缺省函数