匯東網


快速测试一段代码的运行时间

[編輯] [转简体]
|
作者:huidong | 分類:【編程】C/C++
[ 9 瀏覽 0 評論 1 贊 1 踩 ]

概要
快速计时代码运行时间

正文

double dfq;
LARGE_INTEGER fq, t_begin, t_end;
#define TIMEC_INIT    {QueryPerformanceFrequency(&fq);dfq=1.0/fq.QuadPart;}
#define TIMEC_BEGIN    QueryPerformanceCounter(&t_begin);
#define TIMEC_END    {QueryPerformanceCounter(&t_end);printf("%lf\n",(t_end.QuadPart-t_begin.QuadPart)*dfq);}

加在代码里,先TIMEC_INIT(一次即可),开始的时候BEGIN, 结束的时候END会自动输出时间(单位是秒)。


还有个仅DEBUG模式下才输出时间的(VS2019实测):

// 代码耗时计算(仅debug状态下有效)
#ifdef _DEBUG

double dfq;
LARGE_INTEGER fq, t_begin, t_end;
#define TIMEC_INIT    {QueryPerformanceFrequency(&fq);dfq=1.0/fq.QuadPart;}
#define TIMEC_BEGIN    QueryPerformanceCounter(&t_begin);
#define TIMEC_END    {QueryPerformanceCounter(&t_end);printf("%lf\n",(t_end.QuadPart-t_begin.QuadPart)*dfq);}

#else

#define TIMEC_INIT
#define TIMEC_BEGIN
#define TIMEC_END

#endif // _DEBUG


[ 1] [ 1]


 評論區  0 條評論

+ 添加評論