#include <iostream>
#include <time.h>
using namespace std;
int main()
{
clock_t start = clock();
//do some process here
clock_t end = (clock() - start)/CLOCKS_PER_SEC;
cout<<"time comsumption is "<<end<<endl;
}
扩展资料
使用linux的系统设置计时器
#include <sys/time.h>
int main()
{
timeval starttime,endtime;
gettimeofday(&starttime,0);
//do some process here
gettimeofday(&endtime,0);
double timeuse = 1000000*(endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - startime.tv_usec;
timeuse /=1000;//除以1000则进行毫秒计时,如果除以1000000则进行秒级别计时,如果除以1则进行微妙级别计时
}
timeval的结构如下:
strut timeval
{
long tv_sec; /* 秒数 */
long tv_usec; /* 微秒数 */
};