計(jì)算一段代碼執(zhí)行時(shí)間


INT64GetTime_ms(void)
{
#ifdef _MSC_VER
LARGE_INTEGER m_nFreq;
LARGE_INTEGER m_nTime;
QueryPerformanceFrequency(&m_nFreq);
QueryPerformanceCounter(&m_nTime);
return m_nTime.QuadPart *1000/ m_nFreq.QuadPart;
#elif defined(__GNUC__)structtimeval tv_date;
gettimeofday(&tv_date,
NULL);
return(INT64)tv_date.tv_sec *1000+ (INT64)tv_date.tv_usec;
#endif
}
定義三個(gè)時(shí)間參數(shù):
INT32time;
INT64StartTime, EndTime;
使用:
StartTime = GetTime_ms();//在需要測試的代碼前一行計(jì)算開始時(shí)間
EndTime = GetTime_ms();
//結(jié)果代碼的下一行計(jì)算結(jié)束時(shí)間
time = (INT32)(EndTime - StartTime);
//計(jì)算測試代碼的運(yùn)行時(shí)間
printf(
"運(yùn)行時(shí)間 = %d ms\n", time);
/////////////////////////////////////////////////////////////
附:項(xiàng)目中使用到的一個(gè)性能測試類
#ifndef _PERFORMANCE_TEST_
#define _PERFORMANCE_TEST_
#if (!defined(_WIN32) &&!defined(_WIN32_WCE))
#include
#include
#include
#include
#include
#include
#include
#include
#else
#ifdef? _WIN32_WCE
#else
#include
#endif
#include
#include
#endif //(!defined(_WIN32) &&? !defined(_WIN32_WCE))
#include "SNDataTypeDefine.h"
/************************************************************************
*概述:性能測試類.
*功能說明:
*用于需要進(jìn)行性能測試的地方.可以對(duì)代碼塊進(jìn)行性能測試,輸出該代碼段所消耗的時(shí)間以及cpu占用率等信息.
*使用范例:
*PerformanceTest objPerformanceTest;
*objPerformanceTest.begin();//讀取測試起始時(shí)間
*testMethod();//在此處添加待測試的方法
*objPerformanceTest.end();//讀取測試結(jié)束時(shí)間
*objPerformanceTest.printWasteTime();//打印測試使用時(shí)間,以u(píng)s為單位
*objPerformanceTest.printCpuTime();//打印Cpu使用時(shí)間,包括用戶時(shí)間和系統(tǒng)時(shí)間及其占用比率
************************************************************************/
class PerformanceTest
{
public:
PerformanceTest();
~PerformanceTest();
public:
/************************************************************************
*概述:啟動(dòng)測試.
*輸入:
*無
*輸出:
*無
*返回值:
*無
*功能描述:
*在測試程序調(diào)用前先調(diào)用該方法以記錄測試啟動(dòng)時(shí)間.
************************************************************************/
void begin();
/************************************************************************
*概述:結(jié)束測試.
*輸入:
*無
*輸出:
*無
*返回值:
*無
*功能描述:
*在測試程序后調(diào)用該方法以記錄測試結(jié)束時(shí)間.
************************************************************************/
void end();
/************************************************************************
*概述:打印測試程序消耗時(shí)間.精確到微秒(us)
*輸入:
*無
*輸出:
*無
*返回值:
*無
*功能描述:
*打印測試程序消耗時(shí)間.主要用于測試算法,計(jì)算耗時(shí)多少.
************************************************************************/
void printWasteTime();
INT64 getWasteTime();
/************************************************************************
*概述:打印CPU時(shí)間.
*輸入:
*無
*輸出:
*無
*返回值:
*無
*功能描述:
*打印Cpu使用時(shí)間,包括用戶時(shí)間和系統(tǒng)時(shí)間及其占用比率.主要用于測試耗時(shí)較長的功能.
************************************************************************/
void printCpuTime();
private:
#if (!defined(_WIN32) &&? !defined(_WIN32_WCE))
tmsm_tStartTime;//開始時(shí)間
tmsm_tEndTime;//結(jié)束時(shí)間
clock_tm_fStartTime;//開始時(shí)間
clock_tm_fEndTime;//結(jié)束時(shí)間
longclktck;//時(shí)鐘,每秒鐘幾次
timevalm_tvStartTime;//開始時(shí)間
timevalm_tvEndTime;//結(jié)束時(shí)間
#else
LARGE_INTEGERm_Performancefrequency;
unsigned __int64m_startTime;
unsigned __int64m_endTime;
#endif //(!defined(_WIN32) &&? !defined(_WIN32_WCE))
};
#endif //_PERFORMANCE_TEST_
使用:
PerformanceTest performanceTest;
performanceTest.begin();
performanceTest.end();
int wasteTime = (int)(performanceTest.getWasteTime()/1000);


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容