C++ 计算 代码运行时间的 几种方法
2007-07-06 23:14
906 查看
有许多专门的测试工具,测试的准确性很高,本文说的是一些简单的测试方法,这些方法多数是记录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);
}
相关文章推荐
- C++ 计算 代码运行时间的 几种方法
- C++ 计算 代码运行时间的 几种方法 .
- C++ 计算代码运行时间的几种方法(转)
- c/c++计算程序运行时间的几种方法
- Matlab中计算程序显示运行时间的几种方法
- c/c++在windows下获取时间和计算时间差的几种方法总结
- C++中几种测试程序运行时间的方法
- C++利用QueryPerformanceCounter计算代码运行时间
- C++中计算某个代码的运行时间
- C++获取当前时间和计算程序运行时间的方法
- c/c++在windows下获取时间和计算时间差的几种方法总结
- C++获取当前时间和计算程序运行时间的方法
- 【算法与数据结构】关于代码运行时间复杂度的计算方法
- 计算代码片段运行时间的方法 System.Diagnostics.Stopwatch
- c++ 在windows下获取时间和计算时间差的几种方法总结
- Matlab中计算程序运行时间的几种方法
- c++中常用的计算程序运行时间的方法
- c++ 在windows下获取时间和计算时间差的几种方法总结
- 【C++】C++中几种测试程序运行时间的方法
- c/c++在windows下获取时间和计算时间差的几种方法总结