zoukankan      html  css  js  c++  java
  • 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<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('') };
    	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("*********************************************************************************
    "));
    	FileTrace(_T("Test result is as follow :
    time: %I64x, %.3fms, %.3fms
    "), (ULONGLONG)0, Time0, Time1);
    	FileTrace(_T("*********************************************************************************
    "));
        system("pause");
    	return 0;
    }
    test result is :




  • 相关阅读:
    找零钱「Usaco2006 Dec」
    才艺表演「Usaco2018 Open」
    潜入行动「JSOI2018」
    任务安排「SDOI2012」
    BZOJ2298: [HAOI2011]problem a
    JZOJ 5818
    JZOJ 3493
    JZOJ 3470
    JZOJ 5781
    JZOJ 5778
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6869544.html
Copyright © 2011-2022 走看看