C++中计算某个代码的运行时间

来源:互联网 发布:sql regexp replace 编辑:程序博客网 时间:2024/05/16 19:50

不错的小技巧

加入头文件"time.h"

class CMyCountTime   {   private:      clock_t m_time;public:   CMyCountTime(){m_time=clock();}          ~CMyCountTime()       {           m_time=clock()-m_time;   cout<<"运行时间为:"<<(double)m_time/CLOCKS_PER_SEC<<'s'<<endl;   }};


在程序中,将你想要测试的代码写在 CMyCountTime c1;

下面,在析构时就会给出运行时间了

也可以这样,用下面这个格式来写#incldue<time.h>
#include<iostream.h>

int main()

.
.
.
..
      clock_t star=clock(),end(0);
       //你的测试代码
       end=clock();
       cout<<"这段代码运行时间为:"<<double(end-star)<<"ms"<<endl;
.
.
..
.
return 0;
}



 

原创粉丝点击