linux c++ 函数效率,C++高精度性能测试函数

在实际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

#include

#include

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;

}

55cbb38a484b87aeecbb7be5885ce4c3.png

将C语言梳理一下,分布在以下10个章节中:

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值