利用clock()获取cpu的计时单元数,来完成计算程序运行时间
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
int main(void){
clock_t begin,over;
begin = clock();//获取开始时的cup计时单元数
string str = "henuzxy";
for(int i=1;i<=100000000;i++){
int ans = (int)str.length();
}
over = clock();//获取结束时的cup计时单元数
cout << (double)(over - begin)/CLOCKS_PER_SEC << " (s) "<< endl;
//二者相减就是cup的运行周期数,CLOCKS_PER_SECS是cup一秒的运行周期数
return 0;
}