读书人

C++中如何得到某个函数的运行时间。

发布时间: 2012-02-09 18:22:27 作者: rapoo

C++中怎么得到某个函数的运行时间。。。急,在线等!
小弟现在做一课程设计,比较各种排序算法的效率,现在请问怎么得到某个函数的运行时间:比如我在Main()函数中调用shellsort()函数,怎么得出shellsort()函数的运行时间?急,向高手们求教

[解决办法]
我也刚做完啊!今天刚交了,咱们不会是一个学校的吧,呵呵,在函数开始出设置一个clock_t类型的变量start_time,然后start_time=clock();记录下开始时间,结尾处再设一个变量记录,相减强制转换成double然后除以一秒钟内时钟的滴答数CTK_TCK就可以了
double duration=((double)end_time-start_time))/((double)CTK_CLK);
duration就是时间了
[解决办法]
高明一点就是自定义一个计时类
class CMyCountTime
{
private:
clock_t m_time;;
public:
CMyCountTime(){m_time=clock();}
~CMyCountTime()
{
m_time=clock()-m_time;
cout < <m_time/CTK_CLK < <endl;
}
}
测速例子
。。。
{
CMyCountTime mytime;
运行被测试程序代码
}
自动析构得出运行时间
[解决办法]
#include "mmsystem.h "

#pragma comment(lib, "winmm.lib ")


DWORD s = timeGetTime();

你的函数

DWORD d = timeGetTime();

DWORD l = d - s; // 函数执行的时间,毫秒


[解决办法]
如果你需要跟精确,就用查询CUP始终周期吧
[解决办法]
#include "windows.h "

int main()
{
DWORD s = gettickcount();
void functn();
DWORD e = gettickcount();

DWORD dwUsedTimeInMs = e - s;

return 0;
}
[解决办法]
double duration=((double)end_time-start_time))/((double)CTK_CLK)

/////////////////////////////////////////////////////////////////
double duration = (double)Start_time / CLK_TCK ;//注意这里不是CTK_CLK,而是CLK_TCK
这样才能算的出来..........
上面的那个计算结果始终是零,因为如果你调用的这个函数执行的次数很少的话Start_time and End_time这两个是一样的,你将得不到结果,所以为了避免这样,测试的时候最好把循环的次数设置大一些,这样才能有明显的结果.........
[解决办法]
楼上已经说的很清楚了,不过纠正一下,正好也在阅读Windows 核心编程,顺便查阅了下,贴一点资料出来供楼主参考吧

7.5 线程的运行时间

有时想要计算线程执行某个任务需要多长的时间。许多人采取的办法是编写类似下面的代码:


//Get the current time (start time).
DWORD dwStartTime = GetTickCount();

//Perform complex algorithm here.

//Subtract start time from current time to get duration.
DWORD dwElapsedTime = GetTickCount() - dwStartTime;
这个代码做了一个简单的假设:即它不会被中断。但是,在抢占式操作系统中,永远无法知道线程何时被赋予C P U时间。当取消线程的C P U时间时,就更难计算线程执行不同任务时所用的时间。我们需要一个函数,以便返回线程得到的C P U时间的数量。幸运的是,Wi n d o w s提供了一个称为G e t T h r e a d Ti m e s的函数,它能返回这些信息:

BOOL GetThreadTimes(HANDLE hThread,
PFILETIME pftCreationTime, PFILETIME pftExitTime,
PFILETIME pftKernelTime, PFILETIME pftUserTime);
G e t T h r e a d Ti m e s函数返回4个不同的时间值,这些值如表7 - 1所示。

表7-1 GetThreadTimes 函数的返回时间值

时间值 含义
创建时间 用英国格林威治时间1 6 0 1年1月1日午夜后1 0 0 n s的时间间隔表示的英国绝对值,用于指明线程创建的时间
退出时间 用英国格林威治时间1 6 0 1年1月1日午夜后1 0 0 n s的时间间隔表示的英国绝对值,用于指明线程退出的时间。如果线程仍然在运行,退出时间则未定义
内核时间 一个相对值,用于指明线程执行操作系统代码已经经过了多少个1 0 0 n s的C P U时间
用户时间 一个相对值,用于指明线程执行应用程序代码已经经过了多少个1 0 0 n s的C P U时间

使用这个函数,可以通过使用下面的代码确定执行复杂的算法时需要的时间量:


__int64 FileTimeToQuadWord(PFILETIME pft)
{
return (Int64ShllMod32(
pft-> dwHighDateTime, 32) | pft-> dwLowDateTime);
}

void PerformLongOperation()
{
FILETIME ftKernelTimeStart, ftKernelTimeEnd;
FILETIME ftUserTimeStart, ftUserTimeEnd;

FILETIME ftDummy;


__int64 qwKernelTimeElapsed, qwUserTimeElapsed,
qwTotalTimeElapsed;

//Get starting times.
GetThreadTimes(GetCurrentThread(), &ftDummy,
&ftDummy, &ftKernelTimeStart, &ftUserTimeStart);

//Perform complex algorithm here.

//Get ending times.
GetThreadTimes(GetCurrentThread(), &ftDummy,
&ftDummy, &ftKernelTimeEnd, &ftUserTimeEnd);

//Get the elapsed kernel and user times by
//converting the start and end times
//from FILETIMEs to quad words, and then
//subtract the start times from the end times.

qwKernelTimeElapsed =
FileTimeToQuadWord(&ftKernelTimeEnd) -
FileTimeToQuadWord(&ftKernelTimeStart);

qwUserTimeElapsed =
FileTimeToQuadWord(&ftUserTimeEnd) -
FileTimeToQuadWord(&ftUserTimeStart);

//Get total time duration by adding the kernel
//and user times.

qwTotalTimeElapsed = qwKernelTimeElapsed +
qwUserTimeElapsed;

//The total elapsed time is in
//qwTotalTimeElapsed.
}
注意,G e t P r o c e s s Ti m e s是个类似G e t T h r e a d Ti m e s的函数,适用于进程中的所有线程:

BOOL GetProcessTimes(HANDLE hProcess,
PFILETIME pftCreationTime, PFILETIME pftExitTime,
PFILETIME pftKernelTime, PFILETIME pftUserTime);
G e t P r o c e s s Ti m e s返回的时间适用于某个进程中的所有线程(甚至是已经终止运行的线程)。例如,返回的内核时间是所有进程的线程在内核代码中经过的全部时间的总和。
Windows 98 遗憾的是, G e t T h r e a d Ti m e s和G e t P r o c e s s Ti m e s这两个函数在Wi n d o w s9 8中不起作用。在Windows 98中,没有一个可靠的机制可供应用程序来确定线程或进程已经使用了多少C P U时间。

对于高分辨率的配置文件来说, G e t T h r e a d Ti m e s并不完美。Wi n d o w s确实提供了一些高分辨率性能函数:


BOOL QueryPerformanceFrequency(
LARGE_INTEGER* pliFrequency);

BOOL QueryPerformanceCounter(
LARGE_INTEGER* pliCount);
虽然这些函数认为,正在执行的线程并没有得到抢占的机会,但是高分辨率的配置文件是为短期存在的代码块设置的。为了使这些函数运行起来更加容易一些,我创建了下面这个C + +类:

class CStopwatch
{
public:
CStopwatch()
{
QueryPerformanceFrequency(&m_liPerfFreq);
Start();
}

void Start()
{
QueryPerformanceCounter(&m_liPerfStart);
}

__int64 Now() const
{
//Returns # of milliseconds since
//Start was called

LARGE_INTEGER liPerfNow;
QueryPerformanceCounter(&liPerfNow);

return (((liPerfNow.QuadPart -
m_liPerfStart.QuadPart) * 1000)/
m_liPerfFreq.QuadPart);
}

private:

//Counts per second
LARGE_INTEGER m_liPerfFreq;

//Starting count
LARGE_INTEGER m_liPerfStart;
};
使用这个类如下:

//Create a stopwatch timer
//(which defaults to the current time).
CStopwatch stopwatch;

//Execute the code I want to profile here.

//Get how much time has elapsed up to now.
__int64 qwElapsedTime = stopwatch.Now();

//qwElapsedTime indicates how long
//the profiled code executed in milliseconds.

后面介绍的记事方法相当精确了,以前一直在用这种方法

读书人网 >C++

热点推荐