C++ 計算 代碼運行時間的 幾種方法
有許多專門的測試工具,測試的準確性很高,本文說的是一些簡單的測試方法,這些方法多數是記錄CPU的運行時間,沒有考慮操作系統的分時複用,不過不太嚴格的情況都可一用。
1. #include <time.h>
long start=clock(),end(0);
//ToDo:process code
end=clock();
long result=(end-start)/1000
2. windows 平臺
#include <windows.h>
double start=getticktime(),end(0);
//ToDo:process code
end=getticktime();
double result=end-start;
3.windows 平臺
#include <windows.h>
LARGE_INTEGER frequency,start,end;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start);
//ToDO:process code
QueryPerformanceCounter(&end);
double d = (double)(end.QuadPart - start.QuadPart) / (double)frequency.QuadPart * 1000.0;
4.根據線程而來的
CThreadTime ElapsedTime;
ElapsedTime.BeginGetElapsedTime();
//TODO: Your performance code
int nThreadTine = ElapsedTime.EndGetElapsedTime();
該類的實現如下:
// This class is for getting the elapsed thread time of the CPU, the unit is ms
// the usage is:
//
// CThreadTime ElapsedTime;
// ElapsedTime.BeginGetElapsedTime();
// TODO: Your performance code
// int nThreadTine = ElapsedTime.EndGetElapsedTime();
//
#include <Windows.h>
class CThreadTime
{
public:
void BeginGetElapsedTime();
__int64 EndGetElapsedTime();
private:
__int64 FileTimeToQuadWord(PFILETIME pft);
private:
FILETIME ftKernelTimeStart;
FILETIME ftKernelTimeEnd;
FILETIME ftUserTimeStart;
FILETIME ftUserTimeEnd;
FILETIME ftDummy;
};
// Get the time elapsed since the thread start
inline void CThreadTime::BeginGetElapsedTime()
{
GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, &ftKernelTimeStart, &ftUserTimeStart);
}
// Calculate the time elapsed
inline __int64 CThreadTime::EndGetElapsedTime()
{
GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, &ftKernelTimeEnd, &ftUserTimeEnd);
__int64 qwKernelTimeElapsed = FileTimeToQuadWord(&ftKernelTimeEnd) - FileTimeToQuadWord(&ftKernelTimeStart);
__int64 qwUserTimeElapsed = FileTimeToQuadWord(&ftUserTimeEnd) - FileTimeToQuadWord(&ftUserTimeStart);
// Get total time duration by adding the kernel and user times.
// the default is 100ns, so we convert it to ms
return (qwKernelTimeElapsed + qwUserTimeElapsed) / 10000;
}
inline __int64 CThreadTime::FileTimeToQuadWord(PFILETIME pft)
{
return (Int64ShllMod32(pft->dwHighDateTime, 32) | pft->dwLowDateTime);
}
1. #include <time.h>
long start=clock(),end(0);
//ToDo:process code
end=clock();
long result=(end-start)/1000
2. windows 平臺
#include <windows.h>
double start=getticktime(),end(0);
//ToDo:process code
end=getticktime();
double result=end-start;
3.windows 平臺
#include <windows.h>
LARGE_INTEGER frequency,start,end;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start);
//ToDO:process code
QueryPerformanceCounter(&end);
double d = (double)(end.QuadPart - start.QuadPart) / (double)frequency.QuadPart * 1000.0;
4.根據線程而來的
CThreadTime ElapsedTime;
ElapsedTime.BeginGetElapsedTime();
//TODO: Your performance code
int nThreadTine = ElapsedTime.EndGetElapsedTime();
該類的實現如下:
// This class is for getting the elapsed thread time of the CPU, the unit is ms
// the usage is:
//
// CThreadTime ElapsedTime;
// ElapsedTime.BeginGetElapsedTime();
// TODO: Your performance code
// int nThreadTine = ElapsedTime.EndGetElapsedTime();
//
#include <Windows.h>
class CThreadTime
{
public:
void BeginGetElapsedTime();
__int64 EndGetElapsedTime();
private:
__int64 FileTimeToQuadWord(PFILETIME pft);
private:
FILETIME ftKernelTimeStart;
FILETIME ftKernelTimeEnd;
FILETIME ftUserTimeStart;
FILETIME ftUserTimeEnd;
FILETIME ftDummy;
};
// Get the time elapsed since the thread start
inline void CThreadTime::BeginGetElapsedTime()
{
GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, &ftKernelTimeStart, &ftUserTimeStart);
}
// Calculate the time elapsed
inline __int64 CThreadTime::EndGetElapsedTime()
{
GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, &ftKernelTimeEnd, &ftUserTimeEnd);
__int64 qwKernelTimeElapsed = FileTimeToQuadWord(&ftKernelTimeEnd) - FileTimeToQuadWord(&ftKernelTimeStart);
__int64 qwUserTimeElapsed = FileTimeToQuadWord(&ftUserTimeEnd) - FileTimeToQuadWord(&ftUserTimeStart);
// Get total time duration by adding the kernel and user times.
// the default is 100ns, so we convert it to ms
return (qwKernelTimeElapsed + qwUserTimeElapsed) / 10000;
}
inline __int64 CThreadTime::FileTimeToQuadWord(PFILETIME pft)
{
return (Int64ShllMod32(pft->dwHighDateTime, 32) | pft->dwLowDateTime);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.