现在的位置: 首页 > 综合 > 正文

C/C++记录程序运行时间的类

2014年09月05日 ⁄ 综合 ⁄ 共 442字 ⁄ 字号 评论关闭

#ifndef CLK_TCK
#define CLK_TCK CLOCKS_PER_SEC
#endif

template<class _Ty = long>
class TimerRunBase
{
public:
TimerRunBase() : iStart(0), iStop(0) {}
~TimerRunBase() {}

void Start()
{
iStop = iStart = _Ty(clock()) / CLK_TCK;
}

void Stop()
{
iStop = _Ty(clock()) / CLK_TCK;
}

_Ty GetTimer(void)
{
return (iStop - iStart);
}

private:
_Ty iStart;
_Ty iStop;
};

typedef TimerRunBase<> TimerRun;

int main(int argc, char* argv[])
{
TimerRun timer;
timer.Start();
Sleep(10000);
timer.Stop();
timer.GetTimer();
return 0;
}

抱歉!评论已关闭.