//clock() 记录此刻时间,差值计算程序用时
#include<iostream>
#include<cstdlib>
#include<ctime> //头文件
using namespace std;
clock_t t_start,t_end;
int main(){
t_start=clock(); //程序开始计时
int ans=1e8;
while(ans>0){
ans--;
}
t_end=clock(); //程序结束用时
double endtime=(double)(t_end-t_start)/CLOCKS_PER_SEC;//计算
cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位
cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位
system("pause");
return 0;
}
输出样例: