歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++高精度性能測試函數

C++高精度性能測試函數

日期:2017/3/1 9:28:02   编辑:Linux編程

在實際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;
}

C++ Primer Plus 第6版 中文版 清晰有書簽PDF+源代碼 http://www.linuxidc.com/Linux/2014-05/101227.htm

讀C++ Primer 之構造函數陷阱 http://www.linuxidc.com/Linux/2011-08/40176.htm

讀C++ Primer 之智能指針 http://www.linuxidc.com/Linux/2011-08/40177.htm

讀C++ Primer 之句柄類 http://www.linuxidc.com/Linux/2011-08/40175.htm

將C語言梳理一下,分布在以下10個章節中:

  1. Linux-C成長之路(一):Linux下C編程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
  2. Linux-C成長之路(二):基本數據類型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
  3. Linux-C成長之路(三):基本IO函數操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
  4. Linux-C成長之路(四):運算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
  5. Linux-C成長之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
  6. Linux-C成長之路(六):函數要義 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
  7. Linux-C成長之路(七):數組與指針 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
  8. Linux-C成長之路(八):存儲類,動態內存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
  9. Linux-C成長之路(九):復合數據類型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
  10. Linux-C成長之路(十):其他高級議題

Copyright © Linux教程網 All Rights Reserved